Skip to content

Inconsistent inferred type for extended unions. #61616

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Closed
filip131311 opened this issue Apr 25, 2025 · 2 comments
Closed

Inconsistent inferred type for extended unions. #61616

filip131311 opened this issue Apr 25, 2025 · 2 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@filip131311
Copy link

πŸ”Ž Search Terms

when inferring a type for function calls TS is not behaving consistently for extended unions.

πŸ•— Version & Regression Information

I checked TS 5.8.3 and TS 5.8.2

⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0&ssl=22&ssc=2&pln=1&pc=1#code/MYewdgzgLgBAZiEBGGBeGAKAhgJwOYBcMWYAngJRoB8MA3gL4BQoksCIATGjADwAqVbPiJ9KqGg0bNw0GACNcKdPxgBTAB5RVYACYQYAIiwHBuQjFHVajGDBYQQAG1UA6RyDxC85GzAgB3AEsoYAALL3JrW1tgLAhVDCMDcgJfaJhnNm4zAG4YAHp8mFDVHFUYQLA4UrKdGChSAAdykDh4Cv0+NU1tPUNjGAAfQzkDNOj2JAi8wuLS8srqnFr6ppa2sw7urV19JKGRsfTbdg5pgqKSsoqqmtU6huaYVuJ8La6NHb794YNR3yYTGkrHkuC4yg+PV2-QMBz+Ji8IjEVCidhkTlc7k8Zh8tgCwTCEVRMTiCSSKXGtky7XQuQuc2uizuDzWzzabUCnW2vT2A1+-2O8EQUxxM0u8xuSxWj3WrzwWySlKFnHOsyuC1uy3uqyeL02nIs3OhP0OAMY9CAA

πŸ’» Code

const foo1 = (arg: any) => {}
const foo2 = <T>(arg: T) => {}

const bar1 = <T extends "a">(arg: T) =>{
  console.log(arg)
  switch(arg){
    case("a"):
      let f = arg; // here inferred type of f is T extends "a" 
      foo1(arg); // here inferred type of arg is extends "a" 
      foo2(arg); // here inferred type of arg is T extends "a" 
  }
}

const bar2 = <T extends "a" | "b">(arg: T) =>{
  console.log(arg)
  switch(arg){
    case("a"):
      let f = arg; // here inferred type of f is T extends "a" | "b"
      foo1(arg); // here inferred type of arg is "a"
      foo2(arg); // here inferred type of arg is T extends "a" | "b"
  }
}

πŸ™ Actual behavior

arg used as an argument for foo1 inside bar2 is inferred to be of type "a"

or

arg is ever inferred as T extends "a" | "b"

πŸ™‚ Expected behavior

arg used as an argument for foo1 inside bar2 is inferred to be of type T extends "a" | "b"

or

arg is inferred as "a"

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Apr 25, 2025
@RyanCavanaugh
Copy link
Member

All of these results are correct and one or the other is chosen heuristically. Different code doing different things isn't a bug.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" 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 Apr 28, 2025
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

3 participants