在Typescript参数名称中有什么问号

Whi*_*her 140 typescript

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

id?有什么?呢?

Fid*_*kaj 160

将参数标记为可选.

  • 它与TypeScript无关.我在RxJs项目中看到过这样的语法.从doc:它是一个"常见的RxJS约定来识别引用流的变量".https://github.com/redux-observable/redux-observable/blob/master/docs/basics/Epics.md#a-basic-example (8认同)
  • 我在变量名的末尾看到了$符号.那是什么意思? (5认同)
  • @SunilGarg $ postfix 通常是一种命名约定,表示变量是可观察的。 (4认同)

Mas*_*iri 29

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

  • 不完全是。问号的意思是“可选”。所以它是“参数:类型|未定义=未定义”的简写。 (27认同)
  • @lodewykk 这也不完全是一个速记。`param?: type` 将参数声明为可选,`param: type | undefined` 则不然。示例: `const foo: { a?: string } = {}` 有效,但 `const foo: { a: string | undefined } = {}` 失败。同时,在函数声明中@mcoolive是正确的,`arg?: type`相当于`arg: type | 未定义=未定义`,比较/sf/ask/2634293231/#63338805 (18认同)
  • 实际上,它并不是默认为“未定义”,而是默认为不存在。Masih 的答案是正确的:“参数?:类型”是“参数:类型|”的简写。undefined` 请注意 [本示例](https://www.typescriptlang.org/play?ts=3.3.3#code/FAYwNghgzlAEUAsD2AnALgiA7AJgcQFcBPWAb2AEgsIBbAUwC540UBLLAc1gF5YByAGIo6OPpUSoM2HAH4mUFuw4Bucys4B5FAFVcdAGbsR8 中 `= undefined` 的行为有何不同9VWA+sAnsNYRqijgMQC​​YNABUkuxzePM2nWAsrbyMcHktrUNUAX2BgMDo0WH1hRzYQAGtw2wB3eGR0TFxCIgAKAEpVUCQsKCQEgDowJA5SlJE6dIzy4CA)。 (11认同)

DvG*_*DvG 22

这是使变量为Optional类型.如果未使用此变量,则声明的变量显示为" undefined ".

export interface ISearchResult {  
  title: string;  
  listTitle:string;
  entityName?: string,
  lookupName?:string,
  lookupId?:string  
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为正确的名称是"可选参数".可以为Nullable的类型是`string?`.要有一个可选的可空,你要做`name?:string?`. (6认同)
  • 我不同意它表示“可为空”类型。它表示可选,不可为空。例如,上面示例中的“title”的值为“null”仍然有效,但对于声称实现“ISearchResult”的类来说,在编译时缺少“entityName”属性将是无效的。 (5认同)

Wil*_*een 7

?中的参数是表示一个可选参数。Typescript 编译器不需要填写这个参数。更多细节见下面的代码示例:

// baz: number | undefined means: the second argument baz can be a number or undefined

// = undefined, is default parameter syntax, 
// if the parameter is not filled in it will default to undefined

// Although default JS behaviour is to set every non filled in argument to undefined 
// we need this default argument so that the typescript compiler
// doesn't require the second argument to be filled in
function fn1 (bar: string, baz: number | undefined = undefined) {
    // do stuff
}

// All the above code can be simplified using the ? operator after the parameter
// In other words fn1 and fn2 are equivalent in behaviour
function fn2 (bar: string, baz?: number) {
    // do stuff
}



fn2('foo', 3); // works
fn2('foo'); // works

fn2();
// Compile time error: Expected 1-2 arguments, but got 0
// An argument for 'bar' was not provided.


fn1('foo', 3); // works
fn1('foo'); // works

fn1();
// Compile time error: Expected 1-2 arguments, but got 0
// An argument for 'bar' was not provided.
Run Code Online (Sandbox Code Playgroud)