Repository navigation
Defining a type with inferred generic parameters #64938
Description
🔍 Search Terms
infer type generic parameters
Related comment:
Did find some issue about "Higher-kinded types", but none on inference.
Do not confuse this issue with the ones asking for an infer in the generic type contraints.
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Allowing an infer keyword when defining a generic type:
type Y<T extends number> = {};
type X<infer T> = Y<T>;
// equivalent to:
type X<T extends number> = Y<T>The infer keyword would tell TS that the contraints (and default value) of T should be fetched from the definition of Y.
Possible improvement:
When we have multiple generic parameters, it could also be nice to be able to do:
type X<infer ..T> = Y<..T>But IIRC some issues already request features for a ..T.
Possible alternative 1:
Being able to fetch a generic parameter definition from an existing type:
type Y<T extends number> = {};
type X<T extends Y{T}> = Y<T>;
type X<T extends Y{1}> = Y<T>; // starts at 1 if we want 0 to have a special meaning.
type X<T: Y{1}> = Y<T>;Possible alternative 2:
Being able to define generic parameters outside of type definition:
typeparams P = [
T extends number,
U extends string,
];
type Y<..P> = { foo: P.T, faa: P.U };
type X<..P> = Y<..P>;For this one, a current alternative is to use a bag:
type P = {
T: number,
U: string
}
type Y<T extends P> = {foo: T["T"], faa: T["U"]};
type X<T extends P> = Y<T>;However, I'm pretty sure it causes issues when inferring.
📃 Motivating Example
When defining generic types from other generic types, we have to copy/paste all the contraints.
This code duplication is a pain, makes the code harder to read, and can be error prône if the contraints change.
type Y<
T extends A<number>,
U extends B<any>,
V extends C<any>
> = {};
type X<infer T, infer U, infer V> = Y<T, U, V>;
// vs
type X<
T extends A<number>,
U extends B<any>,
V extends C<any>
> = Y<T, U, V>;💻 Use Cases
- What do you want to use this for?
Easily defining generic types from other generic types.
- What shortcomings exist with current approaches?
Lot of duplicated contraints.
- What workarounds are you using in the meantime?
Duplicated contraints.
Activity
MartinJohns commentedon Oct 23, 2026
Duplicate of #29469.
denis-migdal commentedon Oct 24, 2026
The underlying issue is indeed the same, however, the feature requests differ.
I think my proposal would be better.
Initially, they ask for
type X<T> = Y<T>to automatically get the constraint fromY, which may break existing code base :type X<T> = T extends Z ? Y<T> : neverhow should it behave ?A second comment asks for
type X<T> = Y<infer T>, however this would require "flowing the constraints backwards from usages". How should it behave when doing type manipulation onT? Or when doing:type X<T> = Y<infer T> & Z<infer T>?In my proposal, we know from the start that
Tis special. We can simply forbid type manipulations on/with it.I guess we could authorize
type X<infer T> = Y<T> & Z<T>, by making T<T extends Y1 & Z1>.The alternative solution 1:
This entirely solves the issue of inference as we explicitly provide the constraint and default value:
We could also make it stronger:
Note: this also enable to declare parameters