Skip to content

Type narrowing of generic types in control flow analysis#50652

@adrienharnay

Description

@adrienharnay

Bug Report

Hello,

🔎 Search Terms

type narrowing, generic types, control flow analysis

🕗 Version & Regression Information

(see Playground) When trying to narrow generic types (unions), the control flow analysis is not aware of the narrowing. I also see that this PR: #43183 was supposed to address this.

Please keep and fill in the line that best applies:

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type narrowing

⏯ Playground Link

Playground link with relevant code

💻 Code

constINITIAL_STATE={1: 'test1',2: 'test2',};typeState=typeofINITIAL_STATE;conststateReducer=<TypeextendskeyofState>(state: State,action: {type: Type;value: string}|{type: 'clear';value: number},)=>{if(action.type==='clear'){// action.value is "string | number", but should be "number"returnaction.value;}return{ ...state,[action.type]: action.value,};};stateReducer(INITIAL_STATE,{type: 'clear',value: 0});

🙁 Actual behavior

action.value is "string | number"

🙂 Expected behavior

but should be "number"

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions