TypeScript Language Patterns
npx machina-cli add skill hoangnguyen0403/agent-skills-standard/language --openclawTypeScript Language Patterns
Priority: P0 (CRITICAL)
Implementation Guidelines
- Type Annotations: Explicit params/returns. Infer locals.
- Interfaces vs Types:
interfacefor APIs.typefor unions. - Strict Mode:
strict: true. Null Safety:?.and??. - Enums: Literal unions or
as const. No runtimeenum. - Generics: Reusable, type-safe code.
- Type Guards:
typeof,instanceof, predicates. - Utility Types:
Partial,Pick,Omit,Record. - Immutability:
readonlyarrays/objects. Const Assertions:as const,satisfies. - Template Literals:
on${Capitalize<string>}. - Discriminated Unions: Literal
kindproperty. - Advanced: Mapped, Conditional, Indexed types.
- Access: Default
public. Useprivate/protectedor#private. - Branded Types:
string & { __brand: 'Id' }.
Anti-Patterns
- No
any: Useunknown. - 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
- Step 1: Enable strict mode (set strict: true) in tsconfig.json
- Step 2: Annotate function params/returns; use interfaces for APIs and types for unions
- 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