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
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms: conditional type eager, conditional type narrowing
Code
Expected behavior:
No errors,
thas type0 | 1in the function body which gets narrowed to just0in the body of the if statement. In the if statement,dshould have type number, because we know thatthas to have type0.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