Typescript: a problem confuse me

Created on 9 Jun 2017  Â·  3Comments  Â·  Source: microsoft/TypeScript



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:

Question

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manekinekko picture manekinekko  Â·  3Comments

Antony-Jones picture Antony-Jones  Â·  3Comments

wmaurer picture wmaurer  Â·  3Comments

blendsdk picture blendsdk  Â·  3Comments

siddjain picture siddjain  Â·  3Comments