【发布时间】:2022-01-18 22:01:59
【问题描述】:
这段代码有可能存在于 ES5 中并且完美执行:
const ctor = function () {
return {
prop: 'value'
};
};
const obj = new ctor();
console.log(obj);
这是我尝试输入的内容:
interface SomeClass {
new(...args: any[]): { prop: string };
}
const ctor: SomeClass = function () {
return {
prop: 'value'
};
}
const obj = new ctor();
console.log(obj);
这不会在 TypeScript 中编译,它会抛出:
Type '() => { prop: string; }' is not assignable to type 'SomeClass'.
Type '() => { prop: string; }' provides no match for the signature 'new (...args: any[]): { prop: string; }'.(2322)
TypeScript 是故意不支持 ES5 语法还是我遗漏了什么?
【问题讨论】:
-
为什么要添加这个?
new(...args: any[])?您正在使用可变参数定义类型中的函数 -
应该是可以带无参数函数的。顺便说一句,删除并不能解决问题
-
Typescript AFAIK 不支持此功能。 See this
-
@whygee 确实它看起来不受支持