【问题标题】:What is the question mark for in a Typescript parameter nameTypescript 参数名称中的问号是什么
【发布时间】:2016-10-04 14:31:24
【问题描述】:
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;
  }
}

id? 中的? 是干什么用的?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    parameter?: typeparameter: type | undefined 的简写

    那么有什么区别?问号表示“可选”。
    更准确地说parameter?: type等于parameter: type | undefined = undefined

    【讨论】:

    • 不完全是。问号表示“可选”。所以它是“参数:类型|未定义=未定义”的简写..
    • 其实并不默认为undefined,而是默认为不存在。 Masih 的回答是正确的:parameter?: typeparameter: type | undefined 的简写请注意 = undefinedthis example 中的行为有何不同。
    • @lodewykk 这也不完全是简写。 param?: type 将参数声明为可选,param: type | undefined 没有。示例:const foo: { a?: string } = {} 有效,但 const foo: { a: string | undefined } = {} 失败。同时,在函数声明中@mcoolive 是对的,arg?: type 等价于arg: type | undefined = undefined,比较stackoverflow.com/questions/37632760/…
    【解决方案2】:

    【讨论】:

    • 我在变量名的末尾看到了 $ 符号。这是什么意思?
    • 与 TypeScript 无关。我在 RxJs 项目中看到过这样的语法。来自文档:这是“识别引用流的变量的通用 RxJS 约定”。 github.com/redux-observable/redux-observable/blob/master/docs/…
    • @SunilGarg $ 后缀通常是一种命名约定,表示变量是可观察的。
    • 类属性中使用问号怎么办?链接没有解决这个问题。
    【解决方案3】:

    参数中的?表示可选参数。 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.
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 我不同意它表示“可空”类型。它表示可选,不可为空。例如,上面示例中的 title 的值 null 仍然有效,但对于声称实现 ISearchResult 的类在编译时缺少 entityName 属性是无效的.
      • 我认为正确的名称是“可选参数”。可空类型为string?。要拥有一个可选的可空值,您可以使用name?: string?
      • @DvG 考虑到 Josh Gallagher 和 user276648 的 cmets,您介意改进您的答案吗?
      • @user1460043.. 我已经更新了我的答案。感谢您通知我
      • 我假设短语“可选类型”源自 c++、scala 或 python 等语言。你有一个通用的 Optional 类型。实际上,将? 放在类型规范之后而不是变量名之后会更有意义。如果您不向lookupId 传递任何内容,那么它将没有string 类型。
      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 2020-08-03
      • 2021-06-02
      • 1970-01-01
      相关资源
      最近更新 更多