Skip to content

Conditional types are evaluated too eagerly #34810

Closed
@jvanbruegge

Description

@jvanbruegge

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms: conditional type eager, conditional type narrowing

Code

type FooHelper<X> = (arg1: X, arg2: X extends 0 ? number : string) => void;
type Foo = FooHelper<0 | 1>;

let x: number;
const foo: Foo = (t, d) => {
    if(t === 0) { x = d; }
}

Expected behavior:
No errors, t has type 0 | 1 in the function body which gets narrowed to just 0 in the body of the if statement. In the if statement, d should have type number, because we know that t has to have type 0.

Actual behavior:
Type 'string | number' is not assignable to type 'string'.

Playground Link: https://www.typescriptlang.org/play/?ts=3.7-Beta#code/C4TwDgpgBAYg9nAEhANpATgHgBoD4oC8UAFAIboDmAjAFxTYA0U5FATHdlBAB7AQB2AEwDOUAAxQA-FGHB0AS34UodfgFcAtgCMI6AJSF8ANzjzBAbgBQoSLASE7SVBkwSAPlCq4rllBGBQ3HSyCkpWAMZw-LJQAGYIdPBwDsTATIIGBPgA3pZQ+VDysamEBERiBtmBDhZQAL6WdUA

Related Issues: not really

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions