Typescript 为什么';t构造函数属性解析为类';什么是打字脚本?
在以下代码中:Typescript 为什么';t构造函数属性解析为类';什么是打字脚本?,typescript,constructor,Typescript,Constructor,在以下代码中: class A { static f() {} } const y = A; y.f() // OK const x = (new A()).constructor x.f() // type error y的类型是A的类型,但是x的类型是更通用的函数 为什么构造函数属性不能解析为类的类型?我认为这是因为静态关键字。将代码按原样编译到这一行A.f=function(){}没有static关键字,它变成A.prototype.f=function(){} 它看起来像是s
class A {
static f() {}
}
const y = A;
y.f() // OK
const x = (new A()).constructor
x.f() // type error
y
的类型是A的类型,但是x
的类型是更通用的函数
为什么构造函数
属性不能解析为类的类型?我认为这是因为静态
关键字。将代码按原样编译到这一行A.f=function(){}
没有static
关键字,它变成A.prototype.f=function(){}代码>
它看起来像是static
关键字将其附加到类对象,而不是原型链,在原型链中,可以通过调用new
创建对象来访问它。长答案:我认为这不能解决问题。