TypeScript对象实现具有额外属性错误的接口

mis*_*hap 5 typescript

我正在阅读关于TypeScript的Pluralsight课程,这会引发错误,同时它被解释为课程中的有效代码.

错误TS2322:输入'{favouriteSport:string; name:string; 孩子:数量; 年龄:数量; calcPets :()=>数字; makeYo ...'不能指定为'Person'类型.对象文字只能指定已知属性,'person'类型中不存在'favouriteSport'.

interface Person{
    age: number,
    name: string,
    kids: number,
    calcPets: ()=> number;
    makeYounger: (years: number) => void;
    greet: (msg: string) => string; 
}

var p: Person = {
    favouriteSport: "tennis",
    name: "Michael",
    kids: 4,
    age: 44,
    calcPets: function(){
        return this.kids * 2;
    },
    makeYounger: function(years: number){
        this.age -= years;
    },
    greet: function(msg: string){
        return msg + ', ' + this.name;
    }
}
Run Code Online (Sandbox Code Playgroud)

tho*_*epo 2

这些类型的检查是最近在 1.6 中添加的

\n\n
\n

从 1.6 开始,我们\xe2\x80\x99 收紧了一些对象检查规则。[...]\n 您还可以通过传递 --suppressExcessPropertyErrors 编译器选项来抑制此警告。

\n
\n\n

http://blogs.msdn.com/b/typescript/archive/2015/09/02/announcing-typescript-1-6-beta-react-jsx-better-error-checking-and-more.aspx

\n