输入' X'没有与类型' Y'

时间:2017-09-27 13:31:03

标签: typescript

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

export interface IClassHasMetaImplements {
    prototype?: any;
}

export class UserPermissionModel implements IClassHasMetaImplements {
    public test() {
    }
}

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

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

谢谢!

2 个答案:

答案 0 :(得分:15)

正在触发TypeScript' s weak type detection。因为您的接口没有必需的属性,所以从技术上讲,任何类都会满足接口(在TypeScript 2.4之前)。

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

export interface IClassHasMetaImplements {
    prototype?: any;
}

export class UserPermissionModel implements IClassHasMetaImplements {
    public test() {}
    prototype?: any;
}

另见我对前一个答案的评论,这不完全正确。

答案 1 :(得分:0)

接口定义了实现所述接口的对象必须满足的“契约”。

您的IClassHasMetaImplements界面仅定义prototype。通过将test方法添加到UserPermissionModel,您通过尝试添加未在其实现的接口中布局的内容来违反该合同。

您可以向界面添加test,将test设为私有,也可以将其从类中删除以解决错误。

以下是Typescript docs on Interfaces

相关问题