Skip to content

Inconsistent inferred type for extended unions. #61616

Closed as not planned
Closed as not planned
@filip131311

Description

@filip131311

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions