21 cards · Public · Shuffle

TypeScript Masterclass - Advanced Types, Generics & Type Manipulation

Master type-level programming in TypeScript with this comprehensive flashcard deck covering conditional types, mapped types, generics, and core utility types.

Cards in this deck

(21 cards)

Preview terms and definitions before starting your study session.

#1
Term
Union Types vs. Intersection Types
Definition
A Union Type (A | B) describes a value that can be either type A or type B. An Intersection Type (A & B) combines multiple types into one, producing a type that possesses all properties of both A and B simultaneously.
#2
Term
Generic Constraints (extends) Limit Generic Parameters
Definition
Generic constraints restrict the types that can be passed to a generic parameter using the extends keyword.
Example:
typescript3 lines
function getLength<T extends { length: number }>(arg: T): number {
  return arg.length;
}
Here, T must be an object type containing at least a numeric length property.
#3
Term
The keyof Type Operator
Definition
The keyof operator takes an object type and produces a string or numeric literal union of its keys.
Example:
typescript2 lines
type User = { id: number; name: string };
type UserKeys = keyof User; // 'id' | 'name'
#4
Term
Indexed Access Types (T[K]) Look Up Property Types
Definition
Indexed access types look up the specific type of a property on another type using key indexing.
Example:
typescript3 lines
type User = { id: number; name: string; active: boolean };
type NameType = User['name']; // string
type KeyTypes = User['id' | 'active']; // number | boolean
#5
Term
Conditional Types Syntax and Evaluation
Definition
Conditional types select one of two possible types based on a subtyping relationship test.
Syntax:
T extends U ? X : Y
If type T is assignable to type U, the result is type X; otherwise, it evaluates to type Y.
#6
Term
The infer Keyword in Conditional Types
Definition
The infer keyword declares a type variable within the extends clause of a conditional type to extract and infer a subtype.
Example:
typescript2 lines
type UnpackArray<T> = T extends (infer U)[] ? U : T;
type Num = UnpackArray<number[]>; // number
#7
Term
Distributive Conditional Types
Definition
When conditional types act on a generic type parameter that is a union, they automatically distribute over each constituent of that union.
Example:
typescript2 lines
type ToArray<T> = T extends any ? T[] : never;
type Result = ToArray<string | number>; // string[] | number[]
#8
Term
Preventing Distribution in Conditional Types
Definition
To prevent conditional types from distributing over unions, wrap both sides of the extends clause in square brackets [ ].
Example:
typescript2 lines
type NonDistributive<T> = [T] extends [any] ? T[] : never;
type Result = NonDistributive<string | number>; // (string | number)[]
Showing 8 of 21 cards in this deck.
Study All Now