Skip to content

Type Checker Fails to Check Outer Type When Spread Operator is used #55009

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

Closed
MichaelScheffenacker opened this issue Jul 13, 2023 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@MichaelScheffenacker
Copy link

Bug Report

The type checker seems to fail to check the outer type when a spread operator is used .

🔎 Search Terms

repo:microsoft/TypeScript spread type not checked array to object

ts spread operator outer type check fails
ts bug spread operator fails when cast to object instead of array

🕗 Version & Regression Information

Tested on the playground with version 5.1.6, 5.0.4, 4.9.5, 3.9.7

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about spread operator or anything regarding to it. FAG Page.

⏯ Playground Link

TS Playground

There is also this Stack Overflow Question about it.

💻 Code

interface ITest {
  value: number;
};

let x: ITest[] = [{  value: 10 }];

x = { ...x };  // no type check error, but is an object instead of an array
x = { ...[{value: 10}] }  // no type check error, but is an object instead of an array
x = { "0": {value: 10} };  // type check error, is correct
x = { "0": {value: 10}, ...[] };  // no type check error, but is an object instead of an array
x = [ {value: 10} ];  //  no type check error, is correct

🙁 Actual behavior

Type checker does ignore the outer object literal when using the spread operator even though the type is supposed to be an array.

🙂 Expected behavior

Type checker should recognise the outer type if the spread operator is used and raise a mismatched type error.

@MartinJohns
Copy link
Contributor

Duplicate of #52060 / #9726.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 13, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2023
@MichaelScheffenacker
Copy link
Author

@MartinJohns @RyanCavanaugh I see that this is a duplicate of what is discussed in the later parts of #9726 but I do not quite understand why it falls under the same topic as #9726 was initially created for. I see that it might cause the issue in some tangled ways, but would it not still be much easier to fix than the origina #9726 ? The type checker would just have to look at the outer type (Array or Object literal) to decide that there is a type mismatch. The inner type is checked correctly anyways, why not check the out type too? (Sorry, if I have a simplistic view about it, but currently I have no deeper insight about the type checking constraints there ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants