我正在学习TypeScript,我不了解接口的事情。
interface IMyClass extends MyClass {
color: number;
IsResizable: boolean;
title: string;
}
class MyClass {
added: string;
//color: number;
//IsResizable: boolean;
//title: string;
constructor(element: HTMLElement) {
}
test() {
var that: IMyClass = <IMyClass>this; // Error
}
}
我收到了这个错误:
Severity Code Description Project File Line Suppression State
Error TS2352 Neither type 'this' nor type 'IMyClass' is assignable to the other.
Type 'MyClass' is not assignable to type 'IMyClass'.
Property 'color' is missing in type 'MyClass'. TypeScrip app.ts 17 Active
如果取消注释属性,错误就会消失。所以我的问题是:我是否必须将界面属性复制到我的班级?如果我有一个巨大的界面怎么办?还有其他解决办法吗?
答案 0 :(得分:1)
如果真的有意义进行转换,我们可以使用双断言:
// error
var that: IMyClass = <IMyClass>this; // Error
// firstly to any, then to some type
var that: IMyClass = <IMyClass><any>this; // Error
中查看它