-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Typeguard should use conditional types #23764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
this works:
|
I made it work by altering type Pred<T, U extends T> = (v: T) => v is U;
function someFn<T, U extends T>(pred: Pred<T, U>, val: U | Exclude<T, U>): void {
if (pred(val)) {
const u: U = val;
console.log(u);
return;
}
const notU: Exclude<T, U> = val;
console.log(notU);
} if my intuition is off here, i'll gladly close the issue. |
Related to #22348 |
This isn't the case. |
@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". |
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
val
toU
in the "true" branch, and toExclude<T, U>
in the "false" branch.Actual behavior:
In the "false" branch
val
is inferred to beT
:Playground Link: https://bit.ly/2HAtMpF
Related Issues:
The text was updated successfully, but these errors were encountered: