-
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
Supporting generic type inference over the other higher-order functions #9366
Comments
this issue is really problematic for functional paradigm users : const map = <T, U>(transform: (t: T) => U) =>
(arr: T[]) => arr.map(transform)
const identity = <T>(t: T) => t;
const identityStr = (t: string) => t;
const arr: string[] = map(identityStr)(['a']);
const arr1: string[] = map(identity)(['a']); // Type '{}[]' is not assignable to type 'string[]'. |
(welcome back @fdecampredon!) |
(thanks ! :) ) |
Yep, there is to many |
Relevant paper: "Practical type inference for higher-rank types". Specifically the section on subsumption of parametrically polymorphic signatures. Ultimately this comes down to better unification, which is what @gcnew has done some work on if you follow along in #9949. For the |
Another example: const compose = <A, B, C>(g: (b: B) => C, f: (a: A) => B) => (a: A) => g(f(a))
const identity = <T>(x: T) => x
const lessThanTen = (x: number) => x < 10
const composed = compose(lessThanTen, identity)
// ^^^^^^^^^^^
// Argument of type '(x: number) => boolean' is not assignable to parameter of type '(b: {}) => boolean'.
// Types of parameters 'x' and 'b' are incompatible.
// Type '{}' is not assignable to type 'number'. Can be fixed with explicit type parameters const composed = compose<number, number, boolean>(lessThanTen, identity) but would be really nice if this could just unify! |
I would love to know if there is some movement here. Here's another usecase: https://stackoverflow.com/q/47934804/592641 |
I've ran into this issue when using lodash's |
This would also be really valuable for mixins on react components with proptypes export class MyComponent extends MyMixin(React.Component<MyComponentProps>) {} I love the new mixins feature since 2.2 and I was really excited about refactoring my code to create a variety of mixins for my components until I realized as of right now it won't quite work as I'd hoped without a lot of manual type declaring 😭 |
Heads up I put up my attempt at implementing this at #24626. No idea if it'll go anywhere but it works for a lot of the cases I've tried so feel free to try it out if you're interested. |
@kpdonn 😭 It's so beautiful. |
Any progress on this? |
Higher order function type inference now implemented in #30215. |
Currently, TypeScript infers generic types when referring its type immediately. Its behavior is undesirable for using higher-order functions. The type inference of generics should delay until it will be called.
The text was updated successfully, but these errors were encountered: