TypeScript 类的自身类型
需要定义一个 class 类型的非实例变量时,可以用以下格式:
变量名 : typeof 类名;
class 定义了有参数的构造函数时,不可用变量名 : new() => 类名;
变量名 : { new(): 类名 };
当 class 定义了有参数的构造函数时,也需要对应:变量名 : new(name:string) => 类名;
变量名 : { new(name:string): 类名 };
export class Animal { }
export class Dog extends Animal { }
export class Fox extends Animal {
public constructor(name: string) {
super();
}
}
export class Test {
private _animalType1: { new(): Animal };
private _animalType2: new () => Animal;
private _animalType3: typeof Animal;
private _animalType4: new (name: string) => Animal;
private _foxType: typeof Fox;
public constructor() {
this._animalType1 = Animal;
this._animalType1 = Dog;
//this._animalType1 = Fox; // 编译错误:Type 'typeof Fox' is not assignable to type 'new () => Animal'
this._animalType2 = Animal;
this._animalType2 = Dog;
//this._animalType2 = Fox; // 编译错误:Type 'typeof Fox' is not assignable to type 'new () => Animal'
this._animalType3 = Animal;
this._animalType3 = Dog;
//this._animalType3 = Fox; // 编译错误:Type 'typeof Fox' is not assignable to type 'new () => Animal'
this._animalType4 = Animal;
this._animalType4 = Dog;
this._animalType4 = Fox; // 编译通过
this._foxType = Fox;
//this.getName1(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'typeof Animal'
//this.getName2(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'typeof Animal'
//this.getName3(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'typeof Animal'
this.getName4(Fox); // 编译通过
this.getName5(Fox); // 编译通过
//this.getName6(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'new () => Fox'.
//this.getName7(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'new () => Fox'
this.getName8(Fox); // 编译通过
this.getName9(Fox); // 编译通过
}
public getName1(type: { new(): Animal }): string {
return type.prototype.constructor.name;
}
public getName2(type: new () => Animal): string {
return type.prototype.constructor.name;
}
public getName3(type: typeof Animal): string {
return type.prototype.constructor.name;
}
public getName4(type: new (name: string) => Animal): string {
return type.prototype.constructor.name;
}
public getName5(type: { new(name: string): Animal }): string {
return type.prototype.constructor.name;
}
public getName6<T extends Animal>(type: new () => T): string {
return type.prototype.constructor.name;
}
public getName7<T extends Animal>(type: { new(): T }): string {
return type.prototype.constructor.name;
}
public getName8<T extends Animal>(type: new (name: string) => T): string {
return type.prototype.constructor.name;
}
public getName9<T extends Animal>(type: { new(name: string): T }): string {
return type.prototype.constructor.name;
}
// 此方法不可用
// 编译错误:T' only refers to a type, but is being used as a value here.
//public getName10<T extends Animal>(type: typeof T): string {
// return type.prototype.constructor.name;
//}
}
参考:
https://typescript.p6p.net/typescript-tutorial/class.html