-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructing a object using destruction does not maintain type safety #41322
Comments
The contract is not broken. Everything that If you spread a property that is incompatible with You are probably expecting the excess property checks to kick in (which is more of a linter-feature, not part of the type-system). In this case it's a duplicate of #39998. |
You are right. Its a duplicate of #39998. |
from my understanding there seems to be two different rules for assignment:
const foo: {foo: number} = {foo: 1, bar: 2} //error
const foo: () => {foo: number} = ()=>({foo: 1, bar:2}) // no error it seems inconsistent which of these rules is used in many cases ie. my example from #41698. the compiler checks for excess properties when it's in the object directly (rule 1), but not when destructuring another object (rule 2): foo = {...foo, baz: 1 } //Type '{ baz: number; bar: number; }' is not assignable to type 'Foo'. Object literal may only specify known properties, and 'baz' does not exist in type 'Foo'.
foo = {...foo, ...{baz: 1} } // no error |
TypeScript Version: 4.0.3
Code
Expected behavior:
Expected behavior would be for an error to be highlighted underneath
baz
indicating that this property is not part of type Foo.Actual behavior:
No error is shown thus breaking the contract that type
Foo
provides.Playground Link: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChkHIBCcUyAXMgM5hSgDmuAvrrguiDcjJpRlgF4c+YqUoByAG5wANgFcI4gDQiAdOoAUtBcgF7k2lAH4cyAEZwAXpQCMyJhVNMAlAQD0b5AB49QgO4AFgCeyMBUBgFhyLIy6H4QACYm5BrMQA
The text was updated successfully, but these errors were encountered: