Reference · 30 entries

TypeScript Reference

The TypeScript reference: utility types, type operators and keywords with syntax, the version that added each, and examples type-checked by TypeScript 7 that you can run in your browser.

TypeScript 7 Version history

Every example is type-checked with TypeScript 7 (the native compiler) in strict mode and then run, so the output you see is real. Each page shows the TypeScript version that introduced the feature, linked to the TypeScript version history.

Utility Types

TypeScript's built-in utility types: Partial, Required, Readonly, Pick, Omit, Record, Exclude, Extract, ReturnType, Awaited and more, with examples that are type-checked and run.

NameDescription
Partial<Type>Make every property of a type optional. Useful for update functions that accept only the fields that change.
Required<Type>Make every property of a type required, removing the ? from optional properties. The opposite of Partial.
Readonly<Type>Make every property of a type read-only, so the compiler rejects assignments to them after creation.
Pick<Type, Keys>Build a type from just some properties of another type. Handy for API responses and props that need a subset.
Omit<Type, Keys>Build a type with all properties of another type except the ones you list. The opposite of Pick.
Record<Keys, Type>Build an object type whose keys come from Keys and whose values all have Type. Great for lookup tables.
Exclude<UnionType, ExcludedMembers>Remove members from a union type. Exclude<'a' | 'b' | 'c', 'a'> is 'b' | 'c'.
Extract<Type, Union>Keep only the members of a union that are assignable to another type. The opposite of Exclude.
NonNullable<Type>Remove null and undefined from a type, for values you've already checked or that can never be missing.
ReturnType<Type>Get the return type of a function type, so other code can reuse it without repeating it.
Parameters<Type>Get a function's parameter types as a tuple, useful for wrappers that pass arguments through.
Awaited<Type>Unwrap the value type of a Promise (recursively), like what await gives you. Awaited<Promise<number>> is number.
InstanceType<Type>Get the type of the object a class constructor creates, from the constructor's type.
NoInfer<Type>Stop TypeScript from inferring a type parameter from a particular argument, so other arguments decide it.
Uppercase<StringType>Turn a string literal type into upper case at the type level: Uppercase<'get'> is 'GET'.
Capitalize<StringType>Upper-case the first character of a string literal type: Capitalize<'click'> is 'Click'. Used to build names like onClick.

Type Operators and Keywords

keyof, typeof, indexed access, conditional and mapped types, template literal types, satisfies, as const, unknown, never, type predicates and enums, with runnable examples.

NameDescription
keyofProduce a union of an object type's property names: keyof { a: 1; b: 2 } is 'a' | 'b'.
typeof (type query)In a type position, typeof takes the type of a value or function, so you don't have to write it twice.
Type[Key]Look up the type of a property with Type['key'], or of array elements with Type[number].
conditional types (T extends U ? X : Y)Choose between two types based on a condition, like a ternary for types. The basis of many utility types.
inferInside a conditional type, infer captures part of a type into a new type variable, e.g. the element type of an array.
mapped types ({ [K in Keys]: … })Create a new object type by transforming every property of another, the way Partial and Readonly are built.
template literal typesBuild string literal types with template syntax: `on${string}` or combinations of unions like `${Size}-${Color}`.
satisfiesCheck that a value matches a type without widening it, so you keep the exact inferred type and still get errors.
as constMake a value deeply read-only with literal types: strings stay 'admin' instead of string, arrays become readonly tuples.
unknownThe type-safe 'anything': a value of type unknown must be narrowed (checked) before you can use it, unlike any.
neverThe type with no values: for functions that never return, impossible branches, and exhaustiveness checks.
type predicates (x is T)A function returning 'value is Type' tells TypeScript that the value has that type wherever the function returns true.
assertion functions (asserts x is T)A function declared 'asserts value is Type' throws if the check fails, and narrows the type after the call.
enumDefine a set of named constants. Prefer union types or 'as const' objects in new code; use enums when you need a runtime object.

Was this page helpful?