microsoft / microsoft/TypeScript
Request: Infer generics
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
The request is to let infer support generics.
Background:
I recently published a recursive Pipe and Compose. It offers some key advantages, including the preservation of variable names.
https://github.com/babakness/pipe-and-compose-types
Its shortcoming is in the case of generics. This is because infer does not work well with generics. Example
/**
* Extracts function arguments
*/
export type ExtractFunctionArguments < Fn > = Fn extends ( ...args: infer P ) => any ? P : never
/**
* Extracts function return values
*/
export type ExtractFunctionReturnValue<Fn> = Fn extends ( ...args: any[] ) => infer P ? P : never
type Foo = ExtractFunctionArguments< <A>(a:A, b:number) => A >
// Foo has type [ {} , number ]
TypeScript 3.4 improved how pipe and compose functions work with generics with higher order type inference from generic functions.
Common implementations of pipe and compose rely on parameter overloading. However, even here, there is an issue with variadic functions. See example in this issue:
#30727
The broader issue with the parameter overloading version of pipe and compose is that parameter names are lost, decreasing code clarity. The recursive version, relying on infer does maintain parameter names.
Providing support generics with infer will present a major productivity bump and will nearly complete the community's need for the common place pipe and compose in FP patterns without compromise.
The only outlier is a small situation with parameter names on variadic functions of a minimum arity of zero as opposed to at least one. Can provide examples if needed since it is a separate issue.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the generic-function examples in the issue, focusing on how infer handles function arguments and return values. Read the higher-order generic inference context from TypeScript 3.4 and compare the variadic-function discussion in issue #30727. Done means generic types are preserved through infer without losing the intended parameter information.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100