我想编写一个将类实例作为参数的函数,但不允许使用普通的匿名类型的对象。
例如:
class Dog {
constructor(name: string, breed: "husky" | "boxer") {
this.name = name;
this.breed = breed;
}
name: string;
breed: "husky" | "boxer";
}
class Cat {
constructor(name: string, breed: "siamese" | "persian") {
this.name = name;
this.breed = breed;
}
name: string;
breed: "siamese" | "persian";
}
function pat(pet: NoPlainObjects) {
document.write(pet.constructor.name);
}
pat(new Dog('Fido', 'boxer')); //works
pat(new Cat('Spot', 'persian')); //works
pat({name: 'snuffaluffagus'}); //compile error
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
346 次 |
| 最近记录: |