TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)
interface Named {
name: string;
}
let x: Named={ name: 'Alice', location: 'Seattle' };
//let x: Named;
//let y = { name: 'Alice', location: 'Seattle' };
//x = y;(why the code is correct)
// A *self-contained* demonstration of the problem follows...
Expected behavior:
Actual behavior:
In the first line, the object literal is only used to be assigned to x, the property location is most likely an error.
in the second, y aliases the object literal, so the compiler cannot guarantee that location is not needed in other places where y is used. think of var a: Animal = dog; .
Please ask questions like this on StackOverflow in the future.
For the first line's error, see https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.5
You can use compiler options to suppress these errors using --suppressExcessPropertyErrors