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
1---2name: typescript-language-patterns3description: Modern TypeScript standards for type safety, performance, and maintainability.4metadata:5 labels: [typescript, language, types, generics]6 triggers:7 files: ['**/*.ts', '**/*.tsx', 'tsconfig.json']8 keywords:9 [10 type,11 interface,12 generic,13 enum,14 union,15 intersection,16 readonly,17 const,18 namespace,19 ]20---2122# TypeScript Language Patterns2324## **Priority: P0 (CRITICAL)**2526## Implementation Guidelines2728- **Type Annotations**: Explicit params/returns. Infer locals.29- **Interfaces vs Types**: `interface` for APIs. `type` for unions.30- **Strict Mode**: `strict: true`. Null Safety: `?.` and `??`.31- **Enums**: Literal unions or `as const`. **No runtime `enum`**.32- **Generics**: Reusable, type-safe code.33- **Type Guards**: `typeof`, `instanceof`, predicates.34- **Utility Types**: `Partial`, `Pick`, `Omit`, `Record`.35- **Immutability**: `readonly` arrays/objects. Const Assertions: `as const`, `satisfies`.36- **Template Literals**: `on${Capitalize<string>}`.37- **Discriminated Unions**: Literal `kind` property.38- **Advanced**: Mapped, Conditional, Indexed types.39- **Access**: Default `public`. Use `private`/`protected` or `#private`.40- **Branded Types**: `string & { __brand: 'Id' }`.4142## Anti-Patterns4344- **No `any`**: Use `unknown`.45- **No `Function`**: Use signature `() => void`.46- **No `enum`**: Runtime cost.47- **No `!`**: Use narrowing.4849## Code5051```typescript52// Branded Type53type UserId = string & { __brand: 'Id' };5455// Satisfies (Validate + Infer)56const cfg = { port: 3000 } satisfies Record<string, number>;5758// Discriminated Union59type Result<T> = { kind: 'ok'; data: T } | { kind: 'err'; error: Error };60```6162## Reference & Examples6364For advanced type patterns and utility types:65See [references/REFERENCE.md](references/REFERENCE.md).6667## Related Topics6869best-practices | security | tooling