Skip to content
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

Switch inside for loop is converting to intersection #47459

Closed
apacheli opened this issue Jan 15, 2022 · 2 comments
Closed

Switch inside for loop is converting to intersection #47459

apacheli opened this issue Jan 15, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@apacheli
Copy link

apacheli commented Jan 15, 2022

Bug Report

When using a switch statement inside of a loop, it creates weird intersection issues, which should be union types. This causes issues where types are never, or just not correct in general.

🔎 Search Terms

switch case, loop, union intersection, assigning

🕗 Version & Regression Information

TypeScript 4.5

⏯ Playground Link

playground link

💻 Code

  update(data: Partial<Message>) {
    for (const key in data as Message) {
      switch (key) {
        case "content":
        case "tts": {
          const z = data[key]!;
          let g = this[key]!;
          g = z;
          this[key] = data[key]!;
          break;
        }

z is assignable to g, but data[key] is not assignable to this[key] despite being exactly the same type. key is already narrowed down to the correct keys "content" | "tts".

🙁 Actual behavior

When using intelisense, it seems that type is correctly using |. However, when TRULY assigning to this it seems to have created a union instead (& is not correct).

🙂 Expected behavior

It should stay as a union instead.

@apacheli apacheli changed the title Switch inside loop is converting to intersection Switch inside for loop is converting to intersection Jan 15, 2022
@jcalz
Copy link
Contributor

jcalz commented Jan 16, 2022

#30769 is why this is happening, and this issue is probably a duplicate of #32693

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 18, 2022
@typescript-bot
Copy link
Collaborator

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

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