I've added the code that gives me the error to TypeScript's playground, you'll see the error at the object assignment at the bottom.
interface Car {
make: string,
model: string,
year: number
}
function repairCar(car: Car) {
}
let myCar = {
make: 'Honda',
model: 'Accord',
year: 1992,
color: 'red'
}
repairCar(myCar);
But when I add this code:
let myCar2: Car = {
make: 'Honda',
model: 'Accord',
year: 1992,
color: 'red'
}
I get this error:
Type '{ make: string; model: string; year: number; color: string; }' is not assignable to type 'Car'. Object literal may only specify known properties, and 'color' does not exist in type 'Car'.
I don't understand why it is allowed to have the excess property on the object that gets passed to the repairCar function but not at the time when it gets assigned to a variable that has a type annotation, it's the same interface.