本文内容如下
泛型接口 + 泛型函数,泛型工厂函数
如果你都有了答案,可以忽略本文章,或去TS学习地图寻找更多答案
简单知识
// 类
class CommercialBank {
static count: number
constructor(public name: string, public money: number) {
}
}
//双重性质:
1. 当做 类构造函数对象变量 使用
CommercialBank.count
2. 当做 创建类对象的类型 使用
let c = new CommercialBank('农业银行')
工厂函数
// 通用工厂函数类型
type constructorType = new (...arg: any) => any
// 工厂函数
function createInstanceFactory(Constructor: constructorType) {
console.log(Constructor.name)
return new Constructor('工商银行')
}
let result = createInstanceFactory(CommercialBank)
console.log(result); //result是any类型,无法读取属性
泛型工厂函数
createInstanceFactory调用时,传递CommercialBank泛型给constructorType,constructorT