On this page
  1. infer
  2. Syntax & Overview
  3. Notes & Pitfalls
  4. Example

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

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