Lab 01: Type System Limits
Overview
Step 1: Understanding Type Instantiation Depth
// TypeScript has a default depth limit of ~100 recursive instantiations
// Compiler option: --maxNodeModuleJsDepth (different concern)
// SLOW: deeply recursive type
type Repeat<T, N extends number, A extends T[] = []> =
A['length'] extends N ? A : Repeat<T, N, [...A, T]>;
// type R50 = Repeat<string, 50>; // OK
// type R100 = Repeat<string, 100>; // Type instantiation is excessively deep
// FAST: alternative using tail recursion simulation
type BuildTuple<L extends number, T extends any[] = []> =
T['length'] extends L ? T : BuildTuple<L, [...T, unknown]>;
type TupleOf<T, N extends number> =
BuildTuple<N> extends infer U
? { [K in keyof U]: T }
: never;
type Five = TupleOf<string, 5>; // [string, string, string, string, string]Step 2: Type-Level Computation — FizzBuzz
Step 3: Avoiding Slow Type Patterns
Step 4: satisfies Operator (TypeScript 4.9+)
satisfies Operator (TypeScript 4.9+)Step 5: @ts-expect-error vs Assertion Functions
@ts-expect-error vs Assertion FunctionsStep 6: const Type Parameters (TypeScript 5.0)
const Type Parameters (TypeScript 5.0)Step 7: NoInfer (TypeScript 5.4)
Step 8: Capstone — Type-Level FizzBuzz + Runtime Verification
Summary
Feature
Version
Use Case
Last updated
