Class 如何避免TypeScript中的重复接口

Class 如何避免TypeScript中的重复接口,class,typescript,interface,Class,Typescript,Interface,我正在学习打字脚本,但我不了解有关接口的内容 interface IMyClass extends MyClass { color: number; IsResizable: boolean; title: string; } class MyClass { added: string; //color: number; //IsResizable: boolean; //title: string; constructor(e

我正在学习打字脚本,但我不了解有关接口的内容

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
    }
}
如果我取消注释属性,错误将消失。所以我的问题是:我必须将接口属性复制到我的类中吗?如果我有一个巨大的界面呢?还有其他解决方案吗?

如果进行转换确实有意义,我们可以使用双重断言:

// error
var that: IMyClass = <IMyClass>this; // Error
// firstly to any, then to some type
var that: IMyClass = <IMyClass><any>this; // Error
//错误
var that:IMyClass=this;//错误
//首先是任何类型,然后是某种类型

var:IMyClass=

也许阅读手册中的接口文档将有助于澄清接口在Typescript中的作用?也许你能解释一下你想做什么?通常相反,类实现了一个接口。您定义了一个接口,它是MyType的子类。想想看,MzClass没有颜色,但您尝试将其指定给IMyClass@bassarat这本关于打字脚本的书也有一个关于这个主题的非常有用的简短章节。谢谢大家。你的评论帮助了我。是的,我知道双重断言。但TypeScript中的所有内容都是关于强类型变量/对象等的。。。因此,除了使用“我在回答你的问题”…还有其他解决方案吗。。。因为你打破了这个概念。您正试图将一个对象强制转换为另一个对象,而TS编译器告诉您“它无法工作”。所以,我向你们展示了如何在极端情况下行动。。。但总的来说。。避免这样。只要说出真正有意义的东西就行了。。什么满足了所需的接口。。。希望能有点帮助;)好的,谢谢你的帮助。我将尝试重新构造代码,以避免使用任何
// error
var that: IMyClass = <IMyClass>this; // Error
// firstly to any, then to some type
var that: IMyClass = <IMyClass><any>this; // Error