其定义是
interface Type<T> extends Function { new (...args: any[]): T; }
意味着类型Type<T>
的值是也已知为类型T
的实例的constructor的函数.因为它是Function
interface的extends,所以它是Function
,而语法{ new (⋯): ⋯ }
是construct signature,所以它也是一个构造函数.
这意味着如果你有
declare const f: Type<Date>;
然后f
是Function
,并且具有Function
的所有属性,如长度和绑定:
f.apply; // okay
f.call; // okay
f.bind; // okay
f.length; // okay
但它也是Date
个对象的构造函数:
const d = new f();
// ^? const d: Date;
话虽如此,我不知道为什么它是这样定义的.所有构造函数类型自动继承自Function
;您不需要声明它.无论如何,你只能在函数上使用the new
operator.class
es只是一些特殊的功能:
class Foo {};
console.log(typeof Foo) // "function"
这意味着定义
interface Type<T> { new(...args: any[]): T; }
的行为与您的版本相同.也许它是这样定义的,以使与Function
的关系变得明确?不太确定.但这超出了所问问题的范围.
Playground link to code个