前端 - 使用ts的时候,这里的`type?: string` 的问号是什么意思?

 

问题描述:

在使用ts的时候,这里的type?: string 的问号是什么意思?

interface NodeConfig {
  id: string;
  type?: string;
  x?: number;
  y?: number;
  size?: number | number[];
  position?: 'center' | 'top' | 'left' | 'right';
}

 

第 1 个答案:

表示那个字段是可选的

https://www.typescriptlang.or...

Optional Properties

Object types can also specify that some or all of their properties are optional. To do this, add a ? after the property name:

function printName(obj: { first: string; last?: string }) {
// ...
}
// Both OK
printName({ first: "Bob" });
printName({ first: "Alice", last: "Alisson" });

<template lang="pug">.container vue3</template><script setup>import { provide } from ...