You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>'.
I made it work by altering val: T to val: U | Exclude<T, U>, which does not feel too wrong. but my intuition says that by stating <T, U extends T>, T would be equivalent to U | Exclude<T, U>. 🤔
typePred<T,UextendsT>=(v: T)=>v is U;functionsomeFn<T,UextendsT>(pred: Pred<T,U>,val: U|Exclude<T,U>): void{if(pred(val)){constu: U=val;console.log(u);return;}constnotU: Exclude<T,U>=val;console.log(notU);}
if my intuition is off here, i'll gladly close the issue.
Ryan Cavanaugh (@RyanCavanaugh): alright i close the issue then. if you find the time, you can maybe elaborate. My naive reasoning would be: Given there is "Animal" and "Dog", stating: "Animal" would be be the same as "Dog and every Animal but Dog".
Activity
mkulke commentedon Apr 28, 2018
this works:
mkulke commentedon Apr 28, 2018
I made it work by altering
val: Ttoval: U | Exclude<T, U>, which does not feel too wrong. but my intuition says that by stating<T, U extends T>,Twould be equivalent toU | Exclude<T, U>. 🤔if my intuition is off here, i'll gladly close the issue.
mhegazy commentedon Apr 30, 2018
Related to #22348
RyanCavanaugh commentedon Aug 22, 2018
This isn't the case.
mkulke commentedon Aug 23, 2018
Ryan Cavanaugh (@RyanCavanaugh): alright i close the issue then. if you find the time, you can maybe elaborate. My naive reasoning would be: Given there is "Animal" and "Dog", stating: "Animal" would be be the same as "Dog and every Animal but Dog".