Typescript中的符号构造函数错误:[ts]只能使用'new'关键字调用void函数

问题描述 投票:0回答:1

我是打字稿新手,正在尝试使用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个回答
1
投票

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

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

相反,只需删除新的。这将使代码不再引发异常,并使打字稿不再告诉您有问题。

const symbol = Symbol(path);
© www.soinside.com 2019 - 2024. All rights reserved.