TypeScript
TypeScript -> Transpiler (Babel, swc, or Sucrase) -> JavaScript
Union types and type guards
Key Patterns Summary:
1. Discriminated Unions (Most Important!)
type Response =
| { status: "success"; data: any }
| { status: "error"; message: string }
// TypeScript narrows based on 'status'2. Type Guard Operators
typeof x === "string" // Primitives
x instanceof ClassName // Class instances
"property" in x // Property existence
Array.isArray(x) // Arrays3. Custom Type Guards
function isType(x: unknown): x is Type {
return /* check logic */;
}Last updated
Was this helpful?