On this page
Conditional, Mapped & Template Literal Types
Prev: Generics, Constraints & Variance | Up: type_system | Next: Type-Checking & Erasure Pipeline
Rel: infer, typescript/utility_types
Ref: TypeScript Handbook — Mapped Types
Tags: #public #web #typescript #type-system #metaprogramming
Contents
Conditional Types & infer
Conditional types branch on assignability (T extends U ? X : Y) and distribute automatically over naked union type parameters (ToArray<string | number> -> string[] | number[]; wrap in [T] extends [U] to disable distribution):
type UnwrapArray<T> = T extends readonly (infer U)[] ? U : T;
type Item = UnwrapArray<string[]>; // string
Mapped Types & Key Remapping (as)
Transform every key of an object type, adding/removing readonly or ? modifiers (-readonly, -?) and renaming keys with as:
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type UserGetters = Getters<{ id: number; name: string }>;
// { getId: () => number; getName: () => string }
Template Literal Types
Combine unions inside backtick type strings to generate finite string sets:
type Axis = "x" | "y";
type EventName = `pointer:${Axis}:${"start" | "end"}`;
// 'pointer:x:start' | 'pointer:x:end' | 'pointer:y:start' | 'pointer:y:end'
Linked from
- tuts/typescript/tutorials/building_type_level_utilities
- tuts/typescript/type_system
- tuts/typescript/type_system/conditional_and_mapped_types
- tuts/typescript/type_system/generics_and_constraints
- tuts/typescript/type_system/type_checking_and_erasure_pipeline
- tuts/typescript/utility_types
- tuts/typescript/utility_types/function_and_promise
- tuts/typescript/utility_types/function_and_promise/Awaited
- tuts/typescript/utility_types/function_and_promise/ConstructorParameters
- tuts/typescript/utility_types/function_and_promise/InstanceType
- tuts/typescript/utility_types/function_and_promise/NoInfer
- tuts/typescript/utility_types/function_and_promise/Parameters
- tuts/typescript/utility_types/function_and_promise/ReturnType
- tuts/typescript/utility_types/function_and_promise/ThisParameterType
- tuts/typescript/utility_types/object_transforms
- tuts/typescript/utility_types/object_transforms/Omit
- tuts/typescript/utility_types/object_transforms/Partial
- tuts/typescript/utility_types/object_transforms/Pick
- tuts/typescript/utility_types/object_transforms/Readonly
- tuts/typescript/utility_types/object_transforms/Record
- tuts/typescript/utility_types/object_transforms/Required
- tuts/typescript/utility_types/string_manipulation
- tuts/typescript/utility_types/string_manipulation/Capitalize
- tuts/typescript/utility_types/string_manipulation/Lowercase
- tuts/typescript/utility_types/string_manipulation/Uncapitalize
- tuts/typescript/utility_types/string_manipulation/Uppercase
- tuts/typescript/utility_types/union_and_nullability
- tuts/typescript/utility_types/union_and_nullability/Exclude
- tuts/typescript/utility_types/union_and_nullability/Extract
- tuts/typescript/utility_types/union_and_nullability/NonNullable
Related notes
- tuts/typescript/type_system Linked here; links to this page; shared tags: #typescript, #web
- tuts/typescript/type_system/generics_and_constraints Linked here; links to this page; shared tags: #typescript, #web
- tuts/typescript/type_system/type_checking_and_erasure_pipeline Linked here; links to this page; shared tags: #typescript, #web
- tuts/typescript/utility_types Linked here; links to this page; shared tags: #typescript, #web
- tuts/typescript/builtins/infer Linked here; shared tags: #typescript, #web
- tuts/typescript/tutorials/building_type_level_utilities links to this page; shared tags: #typescript, #web
- tuts/typescript/utility_types/function_and_promise links to this page; shared tags: #typescript, #web
- tuts/typescript/utility_types/function_and_promise/Awaited links to this page; shared tags: #typescript, #web
- tuts/typescript/utility_types/function_and_promise/ConstructorParameters links to this page; shared tags: #typescript, #web
- tuts/typescript/utility_types/function_and_promise/InstanceType links to this page; shared tags: #typescript, #web