Typescript: Excess Properties Are Not Checked In Lambdas Without Explicit Return Type
Solution 1:
Finally I found the issue in GitHub, exactly about the problem. In short:
Ideally this would be an error. Unfortunately it turns out to be very difficult to fix this without possibly having consequences in terms of runaway recursion and/or performance
Original answer: Since typescript 1.6, object literals mustn't have extra properties. But if you cast an object to the type, extra properties are allowed. For example:
conststate: CounterState = {
counter: 1,
foo: "bar"// Error, unknown property 'foo'
};
const state2 = {
counter: 1,
foo: "bar"// no errors
} asCounterState
It looks very similar to your problem, when you specify the lambda return type explicitly, the first rule is applied. But, if the return type isn't specified, the compiler thinks: "ok, may be I can cast the object to the CounterState... Is it ok? I'm not sure... But, I will try!", and the second rule is applied.
But I cannot refer to any documentation or compiler specification, which describes such behaviour, I didn't found it too.
Solution 2:
Type compatibility in TypeScript is based on structural subtyping.
https://www.typescriptlang.org/docs/handbook/type-compatibility.html
Typescript designed to allow extra properties. But in some places we have a little inconsistent behaviour and it claims object literal may only specify known properties
. Such behaviour is more expected but it is not a structural subtyping...
Post a Comment for "Typescript: Excess Properties Are Not Checked In Lambdas Without Explicit Return Type"