On this page
infer
Prev: `asserts` | Up: builtins | Next: `any`
Rel: builtins, Structural Typing & Narrowing
Ref: TypeScript Docs — infer
Tags: #public #web #typescript #builtins #tier/specialized
Syntax & Overview
type Elem<T> = T extends readonly (infer U)[] ? U : T;
Declares a type variable to be extracted from within the extends clause of a conditional type.
Notes & Pitfalls
- Only valid inside
extendsconditional types: Extracts a nested type parameter (like the element type of an array or the resolved type of aPromise).
Example
type ElementOf<T> = T extends readonly (infer U)[] ? U : T;
type A = ElementOf<string[]>; // string
type B = ElementOf<number>; // number
Linked from
Related notes
- tuts/typescript/builtins/any Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/asserts Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins Linked here; links to this page; shared tags: #builtins, #typescript, #web
- tuts/typescript/type_system/conditional_and_mapped_types links to this page; shared tags: #typescript, #web
- tuts/typescript/type_system/structural_typing_and_narrowing Linked here; shared tags: #typescript, #web
- tuts/typescript/builtins/as_const shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/enum shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/is shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/keyof shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/namespace shared tags: #builtins, #tier, #typescript, #web