Flashcard Deck · 20 cards · Public

TypeScript - Advanced Types, Generics & Utility Types

Master advanced TypeScript type system mechanics including conditional types, mapped types, utility types, and custom type guards to write bulletproof, flexible type definitions.

Cards in this deck

(20 cards)

Preview terms and definitions before starting your study session.

#1
Term
What does the Partial<T> utility type do?
Definition
Partial<T> constructs a type with all properties of T set to optional (?).
typescript3 lines
type Partial<T> = {
  [P in keyof T]?: T[P];
};
#2
Term
What is the keyof operator in TypeScript?
Definition
The keyof operator takes an object type and produces a string or numeric literal union of its keys.
typescript2 lines
type Point = { x: number; y: number };
type PointKeys = keyof Point; // 'x' | 'y'
#3
Term
What are Mapped Types in TypeScript?
Definition
Mapped types allow you to create new types by iterating over property keys of an existing type using the in keyword.
typescript3 lines
type ReadonlyFlags<T> = {
  readonly [K in keyof T]: boolean;
};
#4
Term
How do Conditional Types work in TypeScript?
Definition
Conditional types select one of two possible types based on a condition expressed as a type relationship test.
Syntax: T extends U ? X : Y
If T is assignable to U, the type evaluates to X; otherwise, it evaluates to Y.
#5
Term
What is the infer keyword used for in Conditional Types?
Definition
The infer keyword allows you to declare and extract a type variable within the extends clause of a conditional type.
typescript2 lines
type ElementType<T> = T extends (infer U)[] ? U : T;
type Num = ElementType<number[]>; // number
#6
Term
What are Template Literal Types?
Definition
Template literal types build on string literal types and expand into union types when interpolated with other literal unions.
typescript3 lines
type Event = 'click' | 'hover';
type EventHandler = `on${Capitalize<Event>}`;
// 'onClick' | 'onHover'
#7
Term
What is a Discriminated Union?
Definition
A pattern where multiple object types share a common single-literal property (the 'discriminant'), allowing TypeScript to narrow down the specific type in a union using a switch or if statement.
typescript3 lines
type Shape = 
  | { kind: 'square'; size: number }
  | { kind: 'circle'; radius: number };
#8
Term
How does the in operator narrow types?
Definition
The in operator checks if a property name exists on an object, serving as a type guard to narrow down union types.
typescript3 lines
if ('radius' in shape) {
  console.log(shape.radius);
}
Showing 8 of 20 cards in this deck.
Study All Now