TypeScript interface implementation object with additional property error

I am taking Pluralsight's TypeScript course and this is causing an error while it is being explained as valid code in the course.

TS2322 error: Type '{favoriteSport: string; name: string; children: number; age: number; calcPets :() => number; makeYo ... 'is not assigned to the type' Person '. An object literal can specify only known properties, and "FavoriteSport" does not exist in the "Face" type.

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; } } 
+5
source share
2 answers

These types of checks were recently added in version 1.6.

Starting at 1.6, some of our object validation rules have been tightened. [...] You can also suppress this warning by passing the -suppressExcessPropertyErrors parameter.

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

+2
source

Take a look at this github issue . Looks like the behavior has changed in 1.6 . I assume that the course you are taking was written before 1.6.

+3
source

Source: https://habr.com/ru/post/1232755/


All Articles