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: NEVER use any. Use unknown or specific interfaces.
- No
Function: Use signature () => void.
- No
enum: Runtime cost.
- No
!: Use narrowing.
- NO LINT DISABLE: PROHIBITED. Fix issues properly.
Testing
- Mocking: Use
jest.Mocked<T> or as unknown as T.
- Checklist: Check method existence, match error constants, satisfy required properties.
- References: See references/TESTING.md for common issues/solutions.
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 and maintainability. Use when working with types, interfaces, generics, enums, unions, or tsconfig settings.4metadata:5 source: https://skillmd.com/skills/hoangnguyen0403/typescript-language-patterns6 labels: [typescript, language, types, generics]7 triggers:8 files: ['**/*.ts', '**/*.tsx', 'tsconfig.json']9 keywords:10 [11 type,12 interface,13 generic,14 enum,15 union,16 intersection,17 readonly,18 const,19 namespace,20 ]21---2223# TypeScript Language Patterns2425## **Priority: P0 (CRITICAL)**2627## Implementation Guidelines2829- **Type Annotations**: Explicit params/returns. Infer locals.30- **Interfaces vs Types**: `interface` for APIs. `type` for unions.31- **Strict Mode**: `strict: true`. Null Safety: `?.` and `??`.32- **Enums**: Literal unions or `as const`. **No runtime `enum`**.33- **Generics**: Reusable, type-safe code.34- **Type Guards**: `typeof`, `instanceof`, predicates.35- **Utility Types**: `Partial`, `Pick`, `Omit`, `Record`.36- **Immutability**: `readonly` arrays/objects. Const Assertions: `as const`, `satisfies`.37- **Template Literals**: `on${Capitalize<string>}`.38- **Discriminated Unions**: Literal `kind` property.39- **Advanced**: Mapped, Conditional, Indexed types.40- **Access**: Default `public`. Use `private`/`protected` or `#private`.41- **Branded Types**: `string & { __brand: 'Id' }`.4243## Anti-Patterns4445- **No `any`**: NEVER use `any`. Use `unknown` or specific interfaces.46- **No `Function`**: Use signature `() => void`.47- **No `enum`**: Runtime cost.48- **No `!`**: Use narrowing.49- **NO LINT DISABLE**: PROHIBITED. Fix issues properly.5051## Testing5253- **Mocking**: Use `jest.Mocked<T>` or `as unknown as T`.54- **Checklist**: Check method existence, match error constants, satisfy required properties.55- **References**: See [references/TESTING.md](references/TESTING.md) for common issues/solutions.5657## Code5859```typescript60// Branded Type61type UserId = string & { __brand: 'Id' };6263// Satisfies (Validate + Infer)64const cfg = { port: 3000 } satisfies Record<string, number>;6566// Discriminated Union67type Result<T> = { kind: 'ok'; data: T } | { kind: 'err'; error: Error };68```6970## Reference & Examples7172For advanced type patterns and utility types:73See [references/REFERENCE.md](references/REFERENCE.md).7475## Related Topics7677best-practices | security | tooling78