类型'X'没有与'Y'类型相同的属性

Mar*_*erl 8 typescript

我将typescript更新到2.5.3版.现在我得到很多打字错误.我有以下简化情况:

export interface IClassHasMetaImplements {
    prototype?: any;
}

export class UserPermissionModel implements IClassHasMetaImplements {
    public test() {
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码声明引发以下错误: error TS2559: Type 'UserPermissionModel' has no properties in common with type 'IClassHasMetaImplements'.

任何人都可以帮我解决这个问题.

谢谢!

Rob*_*ner 17

正在触发TypeScript的弱类型检测.因为您的接口没有必需的属性,所以从技术上讲,任何类都会满足接口(在TypeScript 2.4之前).

要在不更改界面的情况下解决此错误,只需将可选属性添加到您的类:

export interface IClassHasMetaImplements {
    prototype?: any;
}

export class UserPermissionModel implements IClassHasMetaImplements {
    public test() {}
    prototype?: any;
}
Run Code Online (Sandbox Code Playgroud)

另请参阅我对上一个答案的评论,这不完全正确.

  • 我知道这篇文章已经有一段时间了,但它确实节省了我几个小时。谢谢罗伯特! (2认同)