5

I'm going through a Pluralsight course on TypeScript and this throws an error, while it is explained to be a valid code in the course.

error TS2322: Type '{ favouriteSport: string; name: string; kids: number; age: number; calcPets: () => number; makeYo...' is not assignable to type 'Person'. Object literal may only specify known properties, and 'favouriteSport' does not exist in type 'Person'.

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;
    }
}
venerik
  • 5,766
  • 2
  • 33
  • 43
mishap
  • 8,176
  • 14
  • 61
  • 92

2 Answers2

3

Take a look at this github issue. Looks like the behavior changed in 1.6. My guess is the course you are taking was written before 1.6.

leeor
  • 17,041
  • 6
  • 34
  • 60
2

These types of checks were added recently in 1.6

Starting with 1.6, we’re tightening up some of our object checking rules. [...] You can also suppress this warning by passing the --suppressExcessPropertyErrors compiler option.

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

thoughtrepo
  • 8,306
  • 2
  • 27
  • 21