-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type checking stops after spread operator #47995
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
Comments
You're spreading an object typed |
I'm using Angular forms which are not typed. |
You have two assignments, and both are checked. |
The never part is obvious. It just demonstrates the type is of spread is My expectation is that const x: any = { a: 5 }
interface I {
a: string;
b?: string;
}
let spread: I;
spread = {
a: x.a,
b: 5
} The fact that |
What it looks like is that by using spread syntax, the object literal as a whole is inferred as |
@fatcerberus this really explains it.
|
I would say less “bug” and more “design limitation” - TS has to pick a type for the type of the object literal in order to check it (assignability checks are done between types, not values; one assignment = one typecheck), and intersecting anything with One could make the case that |
Treating it as spread = {
...x,
b: 5
} as any |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
It's just unexpected when there's no "temporary" variable that could be thought of as an any type.
will error while following works:
Why would object with spreading be any even if it uses types? |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
🔎 Search Terms
spread
spread operator
spread any
spread object assign
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about the spread operator
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Type mismatch is missed after use of spread operator.
🙂 Expected behavior
I expect a "Type 'number' is not assignable to type 'string | undefined'.(2322)" error for
b: 5
.Similar bug #46128 indicates that after spread the type is not kept.
The main difference between the bugs is that in this case, the type is declared beforehand.
Edit: removed a line of code and changed some wording to make things clearer
The text was updated successfully, but these errors were encountered: