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

const INITIAL_STATE = { 1: 'test1', 2: 'test2', }; type State = typeof INITIAL_STATE; const stateReducer = <Type extends keyof State>( state: State, action: { type: Type; value: string } | { type: 'clear'; value: number }, ) => { if (action.type === 'clear') { // action.value is "string | number", but should be "number" return action.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