最近在使用midway.js框架的时候,遇到一个问题,具体问题已经提交issue: github.com/midwayjs/mi…
请问下面这两种方法有什么区别吗?
class User {
}
type UserType = InstanceType<typeof User>
const user1:User = new User();
const user2:UserType = new User();
具体解释可以看下面这个链接
stackoverflow.com/questions/7…
这里简单解释一下
在ts里定义了一个class,其实还定义了类的实例类型和构造函数类型,这个函数会在我们使用 new创建类实例的时候被调用
- 实例类型
- 方法
- 属性
- User
- 构造函数类型
- 构造函数
- class的静态成员
- typeof User
工厂函数:一个真实的例子
报错的原因:ctor是一个类,而不是一个构造函数。类和构造函数是不同的概念
传入构造函数类型,返回值类型
Because InstanceType helped us declare that the return value type is the constructor's return value type.
返回值是构造函数 返回的值类型,也就是类类型