Skip to content
Skills Library
Programming · Building & coding

typescript-advanced-design-patterns

/typescript-advanced-design-patterns

TypeScript design patterns that use the type system to enforce correctness at compile time — the builder pattern with required-field tracking, type-safe state machines and event emitters, dependency injection, plugin systems, recursive/deep-readonly utility types, type-safe module encapsulation, advanced generic constraints, and typed API clients. Use when structuring a class or module so invalid usage fails to compile rather than throwing at runtime, e.g. "how do I stop `.build()` being called before required fields are set" or "how do I type an event emitter so payloads match their event nam

Repository
tekhne ↗
Licence
MIT
Last repository change
21.10.2026

Assessment and security

95/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
27
Scripts
0
HTML comments
0
URLs in SKILL.md
2
Deprecation

No signals detected. Repository changed on 21.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

TypeScript design patterns that use the type system to enforce correctness at compile time — the builder pattern with required-field tracking, type-safe state machines and event emitters, dependency injection, plugin systems, recursive/deep-readonly utility types, type-safe module encapsulation, advanced generic constraints, and typed API clients. Use when structuring a class or module so invalid usage fails to compile rather than throwing at runtime, e.g. "how do I stop `.build()` being called before required fields are set" or "how do I type an event emitter so payloads match their event nam

Triggers from the description

or
Original author description

TypeScript design patterns that use the type system to enforce correctness at compile time — the builder pattern with required-field tracking, type-safe state machines and event emitters, dependency injection, plugin systems, recursive/deep-readonly utility types, type-safe module encapsulation, advanced generic constraints, and typed API clients. Use when structuring a class or module so invalid usage fails to compile rather than throwing at runtime, e.g. "how do I stop `.build()` being called before required fields are set" or "how do I type an event emitter so payloads match their event nam

How it works

  1. Mindset
  2. When to Apply
  3. Use When
  4. Scope
  5. When NOT to Use
  6. Quick Commands
  7. Anti-Patterns
  8. References
Original SKILL.md headings · 153 lines, 946 words
SKILL.md preview
---
name: typescript-advanced-design-patterns
description: TypeScript design patterns that use the type system to enforce correctness at compile time — the builder pattern with required-field tracking, type-safe state machines and event emitters, dependency injection, plugin systems, recursive/deep-readonly utility types, type-safe module encapsulation, advanced generic constraints, and typed API clients. Use when structuring a class or module so invalid usage fails to compile rather than throwing at runtime, e.g. "how do I stop `.build()` being called before required fields are set" or "how do I type an event emitter so payloads match their event name".
allowed-tools: Read, Write, Edit, Bash
---

# TypeScript Design Patterns

Structural patterns that push correctness into the type system: a class or module shaped so the compiler rejects a misuse the runtime would otherwise have to catch.

## Mindset

A pattern here earns its place only if it converts a runtime failure into a compile error. Before reaching for one, ask what invalid usage it should make impossible to write, not just cleaner to read — a builder that still lets `.build()` run with missing fields is a fluent API, not a type-safe one. Prefer composing the type system's own primitives (discriminated unions, conditional types, mapped types) over hand-rolled runtime checks wherever the invalid state can be excluded at the type level instead of merely caught. Treat generics as free specificity, not decoration: a generic parameter with no constraint is barely more useful than `any`, and a pattern that leaves one unconstrained has not finished its job.

## When to Apply

Use this skill when:

- Enforcing required fields at compile time with the builder pattern
- Modeling application or connection state as a type-safe state machine
- Typing an event emitter so payloads are checked against their event name
- Wiring dependency injection with compile-time-verified dependencies
- Building an extensible plugin system with typed hooks
- Writing recursive utility types (`DeepReadonly`, `DeepPartial`, tree structures)
- Encapsulating a module's internals behind a type-safe public surface
- Building a typed API client from a central route configuration
- Validating form fields (sync and async) with type-safe, cross-field-aware validators
- Applying advanced generic constraints for flexible, reusable code

## Use When

- "How do I stop `.build()` from compiling until every required field is set?"
- "How do I type a plugin system so each plugin's hooks are checked?"
- "How do I model a wizard/form flow so an invalid step transition doesn't compile?"
- "How do I make a deeply nested config object read-only at every level?"

## Scope

### In Scope

- Structural design patterns implemented with TypeScript's type system (builder, state machine, event emitter, DI, plugin system, module encapsulation, API client).
- Recursive and deep utility types (`DeepReadonly`, `DeepPartial`, tree/path types).
- Advanced generic constraint patterns.

### Out of Scope

- Runtime type guards and narrowing mechanics that these patterns call into — see the sibling `typescript-type-guards` skill.
- The underlying conditional/mapped/union type mechanics — see the sibling `typescript-type-system` skill.
- Built-in utility types (`Partial`, `Pick`, `ReturnType`) — see the sibling `typescript-utility-types` skill.

## When NOT to Use

Do not use this skill to look up how a conditional type, mapped type, or discriminated union actually works at the type level — the pattern files assume that mechanic and build on top of it. Do not use it as a substitute for `tsc --noEmit` — a pattern that looks type-safe in a reference file still needs the compiler to confirm it holds for your actual types.

## Quick Commands

### Type Check

```bash
npx tsc --noEmit
```

## Anti-Patterns

### NEVER let a builder's `.build()` compile with missing required fields

**WHY:** a builder pattern's entire value is catching an incomplete construction at compile time; if `.build()` accepts an incomplete state, it is no safer than a plain object literal.

**BAD**:

```typescript
class UserBuilder {
  private name?: string;
  setName(name: string) { this.name = name; return this; }
  build() { return { name: this.name }; } // name may be undefined — compiles anyway
}
```

**GOOD**:

```typescript
class UserBuilder<HasName extends boolean = false> {
  private name?: string;
  setName(name: string): UserBuilder<true> {
    this.name = name;
    return this as UserBuilder<true>;
  }
  build(this: UserBuilder<true>): { name: string } {
    return { name: this.name! };
  }
}
```

### NEVER leave a generic type parameter unconstrained when the pattern relies on it having a shape

**WHY:** an unconstrained `<T>` gives the compiler nothing to check, so the pattern degrades to `any` the moment a caller passes something unexpected.

**BAD**:

```typescript
function createInstance<T>(ctor: T, ...args: any[]) {
  return new (ctor as any)(...args);
}
```

**GOOD**:

```typescript
function createInstance<T extends new (...args: any[]) => any>(
  ctor: T,
  ...args: ConstructorParameters<T>
): InstanceType<T> {
  return new ctor(...args);
}
```

### NEVER type an event emitter's `emit`/`on` with a loose string and `any[]`

**WHY:** the whole point of a typed event emitter is that an event name and its payload are checked together; `emit(event: string, ...args: any[])` throws that guarantee away.

**BAD**:

```typescript
class EventEmitter {
  emit(event: string, ...args: any[]): void { /* ... */ }
  on(event: string, handler: (...args: any[]) => void): void { /* ... */ }
}
```

**GOOD**:

```typescript
class EventEmitter<T extends Record<string, (...args: any[]) => void>> {
  emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>): void { /* ... */ }
  on<K extends keyof T>(event: K, handler: T[K]): void { /* ... */ }
}
```

## References

| File | Covers |
| --- | --- |
| `references/builder.md` | Compile-time enforcement of required fields in the builder pattern |
| `references/state-machine.md` | Discriminated-union state machines with compile-time transition validation |
| `references/event-emitter.md` | Type-safe event names and payloads |
| `references/dependency-injection.md` | Compile-time-verified dependency wiring |
| `references/plugin-system.md` | Extensible, typed plugin architectures |
| `references/deep-readonly.md` | `DeepReadonly`, `DeepPartial`, `DeepRequired`, `DeepMutable`, runtime `deepFreeze` |
| `references/recursive-types.md` | Self-referential types: JSON values, path types, tree structures |
| `references/type-safe-module.md` | Encapsulating module internals behind a typed public surface |
| `references/api-client.md` | Typed API clients built from a central route configuration |
| `references/form-validation.md` | Type-safe, cross-field-aware form validation (sync and async validators) |
| `references/branded-types.md` | Nominal typing with Zod-based validated brands and domain-primitive use cases |
| `references/advanced-generics.md` | Complex, composable generic constraint patterns |

- [TypeScript Handbook: Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html)
- [Refactoring Guru: Design Patterns](https://refactoring.guru/design-patterns)
Files in this package (27)
  • SKILL.md 7 KB
  • .audits/2026-09-23/Analysis.md 1 KB
  • .audits/2026-09-23/Remediation.md 1 KB
  • .audits/2026-09-23/audit.json 1 KB
  • evals/instructions.json 2 KB
  • evals/summary.json 1 KB
  • evals/scenario-0/capability.txt 1 KB
  • evals/scenario-0/criteria.json 1 KB
  • evals/scenario-0/task.md 2 KB
  • evals/scenario-1/capability.txt 1 KB
  • evals/scenario-1/criteria.json 1 KB
  • evals/scenario-1/task.md 2 KB
  • evals/scenario-2/capability.txt 1 KB
  • evals/scenario-2/criteria.json 1 KB
  • evals/scenario-2/task.md 2 KB
  • references/advanced-generics.md 5 KB
  • references/api-client.md 5 KB
  • references/branded-types.md 2 KB
  • references/builder.md 5 KB
  • references/deep-readonly.md 5 KB
  • references/dependency-injection.md 5 KB
  • references/event-emitter.md 4 KB
  • references/form-validation.md 6 KB
  • references/plugin-system.md 5 KB
  • references/recursive-types.md 3 KB
  • references/state-machine.md 7 KB
  • references/type-safe-module.md 3 KB

Versions

The database has no other skills named typescript-advanced-design-patterns.

Database snapshot: 13.11.2026 · package version c4ab8a8cdb8f (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100typescript-advanced-types
    Master TypeScript's advanced type system — generics, conditional types, mapped types, template literals, and utility types — for building type-safe Angular 21.x components, NestJS 11.x APIs, and MCP Builder integrations. Use when implementing complex type inference, type-safe…
  • 100typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type…
  • 100typescript-expert
    Expert in TypeScript including advanced type system features, generics, utility types, and type-safe patterns. Use for TypeScript-specific questions, type errors, and designing type-safe APIs.
  • 100typescript-advanced-patterns
    Advanced TypeScript patterns for type-safe, maintainable code using sophisticated type system features. Use when building type-safe APIs, implementing complex domain models, or leveraging TypeScript's advanced type capabilities.
  • 95typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type…
  • 95typescript-advanced-types
    Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.