Meta-issue: Use Full Unification for Generic Inference?
See original GitHub issueSearch Terms
unification generic inference
Suggestion
Today, TypeScript cannot retain or synthesize free type parameters during generic inference. This means code like this doesn’t typecheck, when it should:
function identity<T>(arg: T) { return arg; }
function memoize<F extends (...args: unknown[]) => unknown>(fn: F): F { return fn; }
// memid: unknown => unknown
// desired: memid<T>(T) => T
const memid = memoize(identity);
Use Cases
Many functional programming patterns would greatly benefit from this.
The purpose of this issue is to gather use cases and examine the cost/benefit of using a different inference algorithm.
Examples
Haven’t extensively researched these to validate that they require unification, but it’s a start:
#9366 #3423 #25092 #26951 (design meeting notes with good examples) #25826 #10247
Issue Analytics
- State:
- Created 5 years ago
- Reactions:95
- Comments:26 (7 by maintainers)
Top Results From Across the Web
TypeScript: implementing pipe function using tuple
Meta-issue: Use Full Unification for Generic Inference? #30134 tracks the overarching problem. This comment may shed light on the mechanism ...
Read more >Type Inference - Learning the Java Language
Compilers from releases prior to Java SE 7 are able to infer the actual type parameters of generic constructors, similar to generic methods....
Read more >Generic unification - Roman Cheplyaka
For instance, I've been working recently on implementing type inference for the nstack DSL, which includes tuples, records, sum types, optionals ...
Read more >other - Go Issues
spec: type inference can't infer types for a generic union of ... all: test failures using proxy.golang.orgNeedsInvestigationproxy.golang.org.
Read more >Strategic directions in constraint programming
infer a constraint on X given constraints ... tially more promising than either full ... the unifying framework of constraints while work-.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@essenmitsosse
This approach would work only for really simple cases. If for example there is any constraint on the type parameter of
identity
we will get an error, and there really is no way to forward the generic type constraint. Also if the number of type parameters is variable we again have an issue, but this could be solved with a number of overloads.@RyanCavanaugh Not sure if this is in scope here, but I have seen people often struggle with generic react components and HOCs. There really is no good way to write a HOC that forwards generic type parameters, maintains generic type constraints and removes some keys from props. Not sure if this will ever be possible but one can dream 😃. A simple example of what I am talking about:
@RyanCavanaugh is there a possibility of taking a look at this issue for the next version? I’ve been running into this issue a lot lately within the react ecosystem.
Example
is there currently a way to overcome this shortcoming without specifying the generic?