-
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
Order of parameters in curried function should not affect inference #16914
Comments
For case 1, TS has no idea what the type should Generics won't be inferred across functions, that is, generics won't be changed once the type parameter was determined. Case 2 should be something like this: -let map2 = <T, U>(xs: T[]) => (fn: (a: T) => U): U[] => {
+let map2 = <T>(xs: T[]) => <U>(fn: (a: T) => U): U[] => { FYI, this is my map declaration.
I think it might be some limitations for recursive function? The FP declaration/inference is really a big challenge to TS, since TS can't infer types across functions. I've tried a lot for Ramda declarations and finally realized that there are a lot of limitations for curried inference. |
Excellent - that works perfectly. It seems that as you said, the current behavior is that once a generic is resolved to a type, it will not change. I wonder if a better behavior would be to refine the generic type as more information comes in, similar to what TS already does for non-generic inferred types. Eg. for the first example: let a = map(_ => _ * 2) // T is inferred as any/{}
let b = a([1, 2, 3]) // T is refined to number (because number is assignable to any/{}) |
Tracking at #30134 since the current inference algorithm is not capable of making 100% order-agnostic inferences |
T
should be fixed atnumber
.map2
's recursive call is inferred incorrectly.On a side note, I'm not sure why the return type annotation
: U[]
is necessary. Without it, the return types of all 3 functions are inferred asany
.Filing all of these as one issue, but I am more than happy to split these out after you guys get a chance to triage.
Related:
The text was updated successfully, but these errors were encountered: