Closed
Description
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'.
Related Issues: not really