Supporting generic type inference over the other higher-order functions #9366
Copy link
Copy link
Closed
Labels
Effort: DifficultGood luck.Good luck.FixedA PR has been merged for this issueA PR has been merged for this issueNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript
Description
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.
function flip<a, b, c>(f: (a: a, b: b) => c): (b: b, a: a) => c {
return (b: b, a: a) => f(a, b);
}
function zip<T, U>(x: T, y: U): [T, U] {
return [x, y];
}
var expected: <T, U>(y: U, x: T) => [T, U] = flip(zip);
var actual: (y: {}, x: {}) => [{}, {}] = flip(zip);Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Effort: DifficultGood luck.Good luck.FixedA PR has been merged for this issueA PR has been merged for this issueNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
fdecampredon commentedon Dec 14, 2016
this issue is really problematic for functional paradigm users :
DanielRosenwasser commentedon Dec 15, 2016
(welcome back François de Campredon (@fdecampredon)!)
fdecampredon commentedon Dec 15, 2016
(thanks ! :) )
baio commentedon Jan 19, 2017
Yep, there is to many
<any>in my Ramda code.Igorbek commentedon Feb 21, 2017
#10247, #9949 for the reference. And see Marin Marinov (@gcnew)'s comment
masaeedu commentedon Jul 31, 2017
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 Marin Marinov (@gcnew) has done some work on if you follow along in #9949. For the
flipexample above, the rank 1 function type<T, U>(x: T, y: U) => [T, U]can be substituted for the rank 0 function type(a: a) => (b: b) => cto obtainf: (x: a, y: b) => [a, b], because the former is more polymorphic than the latter. From there we need TypeScript's usual approach to unification to kick in and inferc=[a, b]. The tricky part is coming up with a general algorithm to do all of this.pelotom commentedon Nov 6, 2017
Another example:
Can be fixed with explicit type parameters
but would be really nice if this could just unify!
Birowsky commentedon Dec 22, 2017
I would love to know if there is some movement here. Here's another usecase: https://stackoverflow.com/q/47934804/592641
cameron-martin commentedon Mar 5, 2018
I've ran into this issue when using lodash's
memoizefunction when the function being memoised is generic.steve8708 commentedon May 3, 2018
This would also be really valuable for mixins on react components with proptypes
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 😭
kpdonn commentedon Jun 2, 2018
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.
nateabele commentedon Jun 2, 2018
Kevin Donnelly (@kpdonn) 😭 It's so beautiful.
sibelius commentedon Jul 20, 2018
Any progress on this?
ahejlsberg commentedon Mar 4, 2019
Higher order function type inference now implemented in #30215.