-
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
Higher order type inference doesn't work with overloads #33594
Comments
@ahejlsberg can you comment? I'm not 100% sure on this one |
This is indeed a known design limitation. From #30215:
We can mark this as a suggestion to support more than one signature in the result function type. It's possible to do, but not trivial. |
Just got hit by this while trying to describe the notion of a generic callable class: class Foo<T> {
constructor(readonly value: T) {}
}
function callable<P extends readonly any[], R>(Constructor: new (...params: P) => R): typeof Constructor & ((...params: P) => R) {
return function (...params) {
return new Constructor(...params);
} as typeof Constructor & ((...params: P) => R);
}
const CallableFoo = callable(Foo);
// => (new (value: any) => Foo<any>) & ((value: any) => Foo<any>)
// Should be: (new <T>(value: T) => Foo<T>) & (<T>(value: T) => Foo<T>)
const foo1 = CallableFoo(42);
// => Foo<any>
// Should be: Foo<number>
const foo2 = new CallableFoo(42);
// => Foo<any>
// Should be: Foo<number> |
@treybrisbane Why not just the following? function callable<A extends readonly any[], R>(ctor: new (...args: A) => R): (...args: A) => R {
return (...args) => new ctor(...args);
} |
@ahejlsberg In my case I'm trying to enable types to be both instantiable via I believe I can work around it. I just figured I'd leave a comment anyway, as I found it confusing that the higher-order inference didn't kick in given that my two overloads are identical in terms of parameters and result. |
@ahejlsberg
TypeScript Version: typescript@3.7.0-dev.20190925
Search Terms:
Code
Also the following doesn't work:
When removing overloads as follows:
or
These work correctly.
Expected behavior:
Infer type parameters correctly and all parameter values are rejected correctly.
Actual behavior:
no error.
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: