Get the FREE Ultimate OpenClaw Setup Guide →

TypeScript Language Patterns

npx machina-cli add skill hoangnguyen0403/agent-skills-standard/language --openclaw
Files (1)
SKILL.md
1.9 KB

TypeScript Language Patterns

Priority: P0 (CRITICAL)

Implementation Guidelines

  • Type Annotations: Explicit params/returns. Infer locals.
  • Interfaces vs Types: interface for APIs. type for unions.
  • Strict Mode: strict: true. Null Safety: ?. and ??.
  • Enums: Literal unions or as const. No runtime enum.
  • Generics: Reusable, type-safe code.
  • Type Guards: typeof, instanceof, predicates.
  • Utility Types: Partial, Pick, Omit, Record.
  • Immutability: readonly arrays/objects. Const Assertions: as const, satisfies.
  • Template Literals: on${Capitalize<string>}.
  • Discriminated Unions: Literal kind property.
  • Advanced: Mapped, Conditional, Indexed types.
  • Access: Default public. Use private/protected or #private.
  • Branded Types: string & { __brand: 'Id' }.

Anti-Patterns

  • No any: Use unknown.
  • No Function: Use signature () => void.
  • No enum: Runtime cost.
  • No !: Use narrowing.

Code

// Branded Type
type UserId = string & { __brand: 'Id' };

// Satisfies (Validate + Infer)
const cfg = { port: 3000 } satisfies Record<string, number>;

// Discriminated Union
type Result<T> = { kind: 'ok'; data: T } | { kind: 'err'; error: Error };

Reference & Examples

For advanced type patterns and utility types: See references/REFERENCE.md.

Related Topics

best-practices | security | tooling

Source

git clone https://github.com/hoangnguyen0403/agent-skills-standard/blob/develop/.github/skills/typescript/language/SKILL.mdView on GitHub

Overview

This skill codifies TypeScript language patterns focused on type safety, performance, and maintainability. It covers choosing interfaces vs types, enabling strict mode, and leveraging advanced features like discriminated unions, utility types, and branded types to write robust code.

How This Skill Works

Developers apply explicit annotations for params and returns, prefer interfaces for APIs and types for unions, and enable strict mode with strict: true. The patterns rely on TypeScript features such as readonly modifiers, as const, satisfies, and branded types to express intent and catch errors at compile time, avoiding runtime costs.

When to Use It

  • Design stable public APIs using interfaces
  • Model configuration objects with strict type checks
  • Refactor code to strict null checks and avoid any
  • Represent complex variants with discriminated unions
  • Enforce immutability with readonly and const assertions

Quick Start

  1. Step 1: Enable strict mode (set strict: true) in tsconfig.json
  2. Step 2: Annotate function params/returns; use interfaces for APIs and types for unions
  3. Step 3: Adopt advanced types and immutability (readonly, as const, satisfies, branded types, discriminated unions)

Best Practices

  • Use explicit params/returns and infer locals
  • Prefer interfaces for API shapes; use types for unions
  • Enable strict mode in tsconfig (strict: true, noImplicitAny)
  • Avoid runtime enums; use literal unions or as const
  • Leverage utility types (Partial, Pick, Omit, Record), mapped/conditional types

Example Use Cases

  • Branded Type: type UserId = string & { __brand: 'Id' }
  • Satisfies (Validate + Infer): const cfg = { port: 3000 } satisfies Record<string, number>
  • Discriminated Union: type Result<T> = { kind: 'ok'; data: T } | { kind: 'err'; error: Error }
  • Immutability: readonly arrays/objects and Const Assertions: as const
  • No runtime enum: prefer literal unions over enums to avoid runtime cost

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers