question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Meta-issue: Use Full Unification for Generic Inference?

See original GitHub issue

Search 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:open
  • Created 5 years ago
  • Reactions:95
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
dragomirtitiancommented, Feb 27, 2019

@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:

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

function HOC<P extends { hocProp: string }>(component: (p: P) => void): (p: Omit<P, 'hocProp'>) => void  {
  return null!;
}

// Works great for regulart components
// const Component: (props: { prop: string }) => void
const Component = HOC(function (props: {  prop: string, hocProp: string}) {});

// Curenly an error, ideally we could write HOC to get 
// const GenericComponent: <T extends number | string>(p: {  prop: T }) => void
const GenericComponent = HOC(function <T extends number | string>(p: {  prop: T, hocProp: string}) {

})
3reactions
lifeiscontentcommented, Aug 24, 2022

@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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found