-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generic symbols without an actual generic type now 'infer' {} instead of any #10990
Comments
Generic type parameters with no inference sites always resulted in The issue here is in TS 2.0 we started narrowing so the issue here is that function isArray(arg: any): arg is Array<any>;
function isArray<T>(arg: any): arg is Array<T>;
function isArray<T>(arg: any): arg is Array<T> {
return true;
} One thing to note, is this will cause loss of safety. I would recommend defining |
I also should note that #9999 is a breaking change. |
Thanks @mhegazy for the quick reply. Your explanation makes sense to me for the isArray case. But why is this failing now: function bug<T>(obj: T): T {
if (obj instanceof RegExp) {
return obj;
}
return null;
} I understand now that obj in the return case is narrowed to RegExp but why is this not assignable anymore to T. obj was T to begin with. |
This is another change, in the past |
We already narrow using intersection for instanceof if the type is not a subtype of the declared type. so we should do the same for generic type arguments as well. |
@RyanCavanaugh @sandersn the issue here is quite ancient. I took a look at the posted examples and I think that all of them behave OK according to today's expectations. What @mhegazy has diagnosed here as something that should be implemented:
also does work OK today. I think it's reasonable to close this issue. |
TypeScript Version: 2.0.2
Code
All this code worked in 1.8.10 but now fails in 2.0.2. From what I can read into the error message the taken type when no actual type parameter is provided is now {}. It seemed to was any before. If I change the code to for example
Everything compiles.
The text was updated successfully, but these errors were encountered: