-
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
Confused type inference with function overload #31578
Comments
In your second overload there is nothing that You can see this when hovering over the |
Thanks for your reply. In my mind, How can I type this, then, so that there isn't a need to explicitly specify the generic types? Is there a way to somehow have a reversed version of |
Could perhaps someone tell me at least if this problem is actually solvable? It might be more obvious for you than it is for me... That would be really helpful already. |
I'm pretty convinced that this problem indeed can't be solved with current TypeScript features. That is because Another version of the problem which is also not solvable at the moment (and which I would like to see TypeScript do at some point) is improving the type safety of existing HTML interfaces by restricting the way HTML hierarchies can be formed. For example: interface HTMLTableElement {
appendChild<T extends HTMLElementTagNameMap['thead'] | HTMLElementTagNameMap['tbody'] | HTMLElementTagNameMap['tfoot']>(newChild: T): T
} This could be solved by giving specific names to all HTML element interfaces, e.g. |
I came across another example which seems to show that type inference is indeed not working properly, which I would like to report as a bug: interface A {
__d: 'A'
}
interface B {
__d: 'B'
}
interface HtmlTypes {
a: A
b: B
}
interface HtmlChildren {
a: void
b: B[]
}
function h<T extends keyof HtmlTypes, E extends HtmlTypes[T]>(e?: E, c?: HtmlChildren[T]) {}
h(null as A, []) // Type inference not working: no errors reported.
h<'a', A>(null, []) // Explicit types given: errors correctly reported. I was trying to introduce a discriminator field to aid TypeScript, but it doesn't seem to be working as expected. Shouldn't this be possible, in the same way that the following is? function h2<T extends keyof HtmlTypes, E extends HtmlTypes[T]>(t?: T, c?: HtmlChildren[T]) {}
h2('a', []) // Type inference working: errors correctly reported. |
Thanks, but no thanks, @typescript-bot. Could you please re-open this issue until someone has time to look at it, @RyanCavanaugh? Thank you. |
TypeScript Version: 3.5.0-dev.20190523
Search Terms: broken type inference / checker in function overloading with generics...
Code
I find it quite difficult to explain the problem in words. In essence, TypeScript is somehow not inferring the generic types correctly. I believe it is best to just look at the code:
Playground Link: here
A secondary problem is that the error are very much not helpful in the snippet above. They become useful when the order of the overloads is reversed:
Playground Link: here
Related Issues: #27972, #20396, #10645, #21707
The text was updated successfully, but these errors were encountered: