TypeScript Version: 2.9.0-dev.20180428
Search Terms:
conditional types, type guards
Code
typePred<T,UextendsT>=(v: T)=>v is U;functionsomeFn<T,UextendsT>(pred: Pred<T,U>,val: T): void{if(pred(val)){constu: U=val;console.log(u);return;}constnotU: Exclude<T,U>=val;// Type 'T' is not assignable to type 'Exclude<T, U>'.console.log(notU);}Expected behavior:
The code should compile, the predicate type-guard sets val to U in the "true" branch, and to Exclude<T, U> in the "false" branch.
Actual behavior:
In the "false" branch val is inferred to be T:
Type 'T' is not assignable to type 'Exclude<T, U>'.
Playground Link:https://bit.ly/2HAtMpF
Related Issues:
TypeScript Version: 2.9.0-dev.20180428
Search Terms:
conditional types, type guards
Code
Expected behavior:
The code should compile, the predicate type-guard sets
valtoUin the "true" branch, and toExclude<T, U>in the "false" branch.Actual behavior:
In the "false" branch
valis inferred to beT:Playground Link:https://bit.ly/2HAtMpF
Related Issues: