Typescript 参数名称中的问号是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37632760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:10:21  来源:igfitidea点击:

What is the question mark for in a Typescript parameter name

typescript

提问by Whisher

export class Thread {
  id: string;
  lastMessage: Message;
  name: string;
  avatarSrc: string;

  constructor(id?: string,
              name?: string,
              avatarSrc?: string) {
    this.id = id || uuid();
    this.name = name;
    this.avatarSrc = avatarSrc;
  }
}

In id?what's the ?for?

是为了id?什么?

回答by DvG

This is to make the variable of Optional type.Otherwise declared variables shows "undefined" if this variable is not used.

这是为了使变量为 Optional 类型。否则,如果未使用此变量,则声明的变量将显示“未定义”。

export interface ISearchResult {  
  title: string;  
  listTitle:string;
  entityName?: string,
  lookupName?:string,
  lookupId?:string  
}

回答by Masih Jahangiri

parameter?: typeis a shorthand for parameter: type | undefined

parameter?: type是一个简写 parameter: type | undefined