-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Type guards don't infer generic types #39727
Labels
Working as Intended
The behavior described is the intended behavior; this is not a bug
Comments
RyanCavanaugh
added
the
Working as Intended
The behavior described is the intended behavior; this is not a bug
label
Jul 24, 2020
This is a correct error for the reason described in the error message. // Causes the value '1' to inhabit an expression of type '2'
const a = func<2>(2); |
@RyanCavanaugh Perhaps that was a wrong example. My actual code is similar to: function func<T>(object: T): T extends Number ? number : T {
if(object instanceof Number) {
return object.valueOf();
} else {
return object;
}
} I don't see how I'll reopen the issue and change the code in the first comment if this turns out to be a bug. |
Sorry for duplicates. I'll try to search better next time. |
Those ones are hard for me to find 😅 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 4.0.0-dev.20200724
Search Terms: generics, type guard, typeof
Code
Expected behavior:
The code compiles. It is obvious that if
typeof object === "number"
, thenT
must benumber
, and1
is anumber
.Actual behavior:
The code yields:
Playground Link: There we go
Related Issues: none
The text was updated successfully, but these errors were encountered: