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
What is the question mark for in a Typescript parameter name
提问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 Fidan Hakaj
It is to mark the parameter as optional.
是将参数标记为可选。
回答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?: type
is a shorthand for parameter: type | undefined
parameter?: type
是一个简写 parameter: type | undefined