【问题标题】:Symbol Constructor error in Typescript: [ts] Only a void function can be called with the 'new' keywordTypescript 中的符号构造函数错误:[ts] 只能使用“new”关键字调用 void 函数
【发布时间】:2020-02-26 07:40:26
【问题描述】:

我是 typescript 的新手,正在尝试使用 ES6 Symbol 构造函数。如何在不使用 any 的情况下正确解决此 ts lint 问题?

        const symbol = new Symbol(path);

我不想做的事:

        const symbol = new (Symbol as any)(path);

【问题讨论】:

    标签: typescript ecmascript-6 types symbols new-operator


    【解决方案1】:

    您不会将new 与符号一起使用,打字稿会提醒您注意这一事实。您的代码(有或没有as any)在运行时会引发异常:

    const path = 'something';
    const symbol = new Symbol(path);

    相反,只需放下新的。这将使代码不再抛出异常,并使 typescript 不再告诉你有问题。

    const symbol = Symbol(path);
    

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多