-
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
Inconsistent Type Inference in Generic Function #59638
Comments
The first one doesn't really infer The second one is different because at the time that you get to inferring |
You can add |
Yes, you can see now how the first function doesn't infer Your post implied that both are largely the same but they are not. I'm not saying that it's impossible to improve this - that I don't know ;p but those two situations deal with a different level of complexity. My educated guess is that if it would have to be improved it would have to be done here |
Thank you for your explanation. I guess my question is, if this is an expected behavior or a bug, and if there is anything I can do right now to make |
While it would be nice for this to work, we'd need #30134 to make this work in the general case where one type parameter depends on the resolution of the previous one. The current algorithm isn't well equipped to insert an extra round of inference here to settle on The example is a bit under-specified to give more advice. As written it seems kind of ambiguous, e.g. interface URItoKind<A> {
readonly ['Maybe1']: Maybe<[A, unknown]>;
readonly ['Maybe2']: Maybe<[unknown, A]>;
}
type URIS = keyof URItoKind<any>;
type Kind<URI extends URIS, A> = URItoKind<A>[URI];
class Maybe<A> {
_inferHelper!: A;
static of: <A>(a: A) => Maybe<A>;
}
declare function test1<F extends URIS, A, B>(f: (a: A) => Kind<F, B>): [F, B, Maybe<B>];
declare function test2<F extends URIS>(): <A, B>(f: (a: A) => Kind<F, B>) => [F, B, Maybe<B>];
declare const func: (x: number) => Maybe<[string, number]>;
const t1 = test1(func); // Maybe<unknown> - Doesn't work
const t2 = test2()(func)[2]; |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
"type inference", "generics", "generic function", "generic type inference", "unknown inference", "generic unknown"
🕗 Version & Regression Information
⏯ Playground Link
link
💻 Code
🙁 Actual behavior
Type inference does not work for
test1
.🙂 Expected behavior
I see no reason why type inference shouldn't work here same as in
test2
.The text was updated successfully, but these errors were encountered: