Flashcard Deck · 20 cards · Public

TypeScript - Advanced Type Systems, Generics & Utility Types

Master modern TypeScript with this high-yield flashcard deck covering advanced type inference, generics, utility types, mapped types, and production-grade software engineering patterns.

Cards in this deck

(20 cards)

Preview terms and definitions before starting your study session.

#1
Term
How does unknown differ from any in TypeScript?
Definition
any disables all type-checking, allowing arbitrary property access and operations without safety. unknown is the type-safe counterpart; it represents any value, but TypeScript prevents operations on an unknown variable until it is narrowed through type guards or type assertion.
#2
Term
What is the never type and when is it used?
Definition
The never type represents the type of values that never occur. It is used for functions that never return (e.g., throw errors or infinite loops) and to enforce exhaustiveness checking in conditional branches or discriminated unions.
#3
Term
How do typeof and instanceof narrow types in TypeScript?
Definition
  • typeof: Narrows primitive types at runtime (e.g., typeof x === 'string'). Valid return strings are 'string', 'number', 'bigint', 'boolean', 'symbol', 'undefined', 'object', and 'function'.
  • instanceof: Narrows object types by checking if an object's prototype chain contains a constructor function (e.g., err instanceof Error).
#4
Term
What is a User-Defined Type Guard (Type Predicate)?
Definition
A function that performs a runtime check and returns a boolean, with a special return type signature arg is Type that informs the compiler to narrow arg within conditional blocks.
typescript3 lines
function isFish(pet: Fish | Bird): pet is Fish {
  return (pet as Fish).swim !== undefined;
}
#5
Term
How does the in operator perform type narrowing?
Definition
The in operator checks if a property exists on an object type, narrowing union types based on property presence.
typescript8 lines
type Admin = { name: string; privileges: string[] };
type User = { name: string };

function checkRole(account: Admin | User) {
  if ('privileges' in account) {
    // account is narrowed to Admin
  }
}
#6
Term
What is a Discriminated Union (Tagged Union)?
Definition
A pattern where multiple object types share a common literal property (the 'discriminant'), allowing TypeScript to automatically narrow down the specific type based on checks against that property.
typescript3 lines
type Circle = { kind: 'circle'; radius: number };
type Square = { kind: 'square'; sideLength: number };
type Shape = Circle | Square;
#7
Term
How is the never type used for exhaustiveness checking?
Definition
By assigning an unhandled branch in a switch or if statement to a variable of type never, TypeScript will raise a compile-time error if a new union member is added but not handled.
typescript9 lines
function getArea(shape: Shape): number {
  switch (shape.kind) {
    case 'circle': return Math.PI * shape.radius ** 2;
    case 'square': return shape.sideLength ** 2;
    default:
      const _exhaustiveCheck: never = shape; // Compiler error if new shape added
      return _exhaustiveCheck;
  }
}
#8
Term
What is a Generic Constraint and how is it declared?
Definition
A generic constraint restricts the types that can be passed to a generic type parameter using the extends keyword.
typescript3 lines
function getLength<T extends { length: number }>(arg: T): number {
  return arg.length;
}
Showing 8 of 20 cards in this deck.
Study All Now

Related Flashcard Sets

Explore similar study materials created by the community

Browse community decks