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.

Optional Generic Type Inference

See original GitHub issue

#13487 added default generic types, but it’s still not possible to infer a generic type:

type Return<T extends () => S, S = any> = S

Here S takes its default type any, but T could permit inference of a subset type of any for S:

const Hello = () => 'World'

type HelloReturn = Return<typeof Hello> // any

Here HelloReturn still has any type, and TypeScript could infer S as the literal type 'World', which is a subset of any.

Use Case

Here’s an example of a fully declarative workaround for #6606, using generic type inference:

type Return<T extends () => S, S = any> = S
const Hello = () => 'World'

type HelloReturn = Return<typeof Hello>  // 'World'

Default Type

If return of T is not a subset of S, it should throw an error:

type ReturnString<T extends () => S, S = string> = S
const Hello = () => 'World'
type HelloReturn = ReturnString<typeof Hello>  // 'World'
const calculateMeaningOfLife = () => 42
type MeaningOfLife = ReturnString<typeof calculateMeaningOfLife> // ERROR: T should return a subset of String

Multiple Possible Type Inferences

The current implementation always returns the superset (which is just the default generic type), solving this issue would require to return the subset (the most precise type of all the inferred possibilities).

If a type has multiple possible type inferences, TypeScript should check that all these types are not disjoint, and that they all are subsets of the Default Generic Type.

The inferred type is the most precise subset.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:190
  • Comments:46 (23 by maintainers)

github_iconTop GitHub Comments

53reactions
EvanMachusakcommented, Mar 27, 2017

Yeah, yeah, but John, your language designers were so preoccupied with whether or not they could that they stopped to wonder whether or not they should.

Typescript is beginning to delve into the territory of too complicated. This is a hard shove off a cliff, in my opinion. Generic syntax is relatively well understood. Generic constraints are relatively well understood. Generic constraints in the form of a pair of lambda expressions would confuse almost everyone who’s seen a generic in another language before.

18reactions
niieanicommented, Mar 27, 2017

JavaScript’s a dynamically typed language, and therefore it requires levels of expression in typing that statically typed languages don’t. Nobody is forcing you to use these advanced ways of typing, better yet, you could even create a linter rule that forbids their usage.

On the other hand, this feature would enable proper and stricter typings for so many real-world use-cases that it would be very welcome, even if used only for typing external libraries @DefinitelyTyped. Moreover, I’ve mentioned two related, complex cases, in which this proposal trumps the necessity for additional complexity, i.e. it proposes to make the language simpler, rather than more complex.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: infer type of generic after optional first generic
Right now you either have to specify all type parameters manually or let the compiler infer all type parameters; there's no partial inference....
Read more >
Generic Return Type, Optional Parameter and Default Value
The type is optional since TypeScript can infer it from the assignment which is always present by default, but it's still a good...
Read more >
Is it possible to have an optional generic inference that obeys ...
Hi! I have a custom struct that have 3 optional generic views (so I can use as Text, Navigation Item, Button or anything):...
Read more >
Documentation - Generics - TypeScript
Working with Generic Type Variables ... When you begin to use generics, you'll notice that when you create generic functions like identity ,...
Read more >
Typescript: Type Inference on function arguments (huge update)
It works, but sometimes, if not always, you are not allowed to use immutable values. Second option is much better. You can add...
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