interface A {
name: string;
}
class MyClass implements A {
name: string;
constructor(name: string) {
this.name = name;
}
}
let myObject: A = new MyClass('example');
if (myObject instanceof MyClass) {
let myInstance = myObject as MyClass;
console.log('myObject is an instance of MyClass');
console.log(myInstance.name);
} else {
console.log('myObject is not an instance of MyClass');
}
示例代码