ts-oop
ACTIVATE when designing TypeScript classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, Tell Don't Ask, or Symbol.iterator. Provides TS-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus TS-specific patterns (Symbol.iterator, branded types for primitive identifiers). DO NOT use for: a value object or an aggregate modelled as an immutable record rather than a class (see ddd-ts-fp), refactoring methodology (see ts-refactoring).
- License 许可
- MIT
- 最近更新
- 2026-10-13
- Stars
- ★ 9
- Forks
- 2
01 前言 Front Matter
- name
- ts-oop
- description
- ACTIVATE when designing TypeScript classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, Tell Don't Ask, or Symbol.iterator. Provides TS-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus TS-specific patterns (Symbol.iterator, branded types for primitive identifiers). DO NOT use for: a value object or an aggregate modelled as an immutable record rather than a class (see ddd-ts-fp), refactoring methodology (see ts-refactoring).
02 资源文件 6
- 参考文档steps/step-01-validate-prerequisites.md9.8 KB
- 参考文档steps/step-02-design-epics.md7.1 KB
- 参考文档steps/step-03-create-stories.md8.2 KB
- 参考文档steps/step-04-final-validation.md4.4 KB
- 参考文档templates/epics-template.md1.1 KB
- 参考文档workflow.md2.9 KB
03 SKILL.md 正文
OOP — TypeScript Examples
The rules are defined in
craft:oop-principles. This skill provides TypeScript / NestJS examples plus TS-specific patterns (Symbol.iterator, branded types).
See also:
ts-conventionsfor typing rules (strict mode, etc.).
Domain models under
features/<feature>/domain/are typed, not classed —frontend-clean-architectureowns that shape. The classes below apply to value objects and collections outside that layer.
Example — Rule 1: Tell Don't Ask
// ❌ AVOID — caller interrogates and decides
const requiredFields = identityDocument.getRequiredFieldNames();
for (const fieldName of requiredFields) {
const file = identityDocument.getFileByFieldName(fieldName);
if (file !== null) continue;
const existing = existingFiles.find(fieldName);
if (existing === null) continue;
// ... re-download logic
}
// ✅ CORRECT — object exposes behavior
for (const [fieldName, existingFile] of identityDocument.getMissingExistingFiles()) {
// Object already determined which files are missing
}
Example — Rule 2: Collection Over Named Properties
// ❌ AVOID — N separate fields with parallel handling
class FormData {
private frontFile: File | null = null;
private backFile: File | null = null;
private passportFile: File | null = null;
getFileByFieldName(name: string): File | null {
switch (name) {
case 'front_file': return this.frontFile;
case 'back_file': return this.backFile;
case 'passport_file': return this.passportFile;
default: return null;
}
}
}
// ✅ CORRECT — one keyed collection
class FormData {
private readonly files = new Map<string, File>();
addFile(fieldName: string, file: File): void {
this.files.set(fieldName, file);
}
getFiles(): ReadonlyMap<string, File> {
return this.files;
}
}
Example — Rule 3: Whole Object
// ❌ AVOID — caller destructures
collection.add({
documentType: document.type,
documentName: document.originalFileName,
downloadUrl: url,
});
// ✅ CORRECT — pass the whole object
collection.addFromDocument(document, url);
Example — Rule 4: Iterable Collections via Symbol.iterator
TS-specific: implement Symbol.iterator to allow for...of while keeping internals private.
class FilesCollection implements Iterable<[string, FileInfo]> {
private readonly files = new Map<string, FileInfo>();
add(name: string, file: FileInfo): void {
this.files.set(name, file);
}
[Symbol.iterator](): Iterator<[string, FileInfo]> {
return this.files.entries();
}
}
// Clean usage
for (const [name, file] of collection) { /* ... */ }
Example — Rule 5: Self-Descriptive Value Object
// ❌ AVOID — consumer needs an external mapping
class UploadFile {
constructor(
readonly content: Buffer,
readonly originalFileName: string,
) {}
}
// Controller maintains fieldName → fileType outside the object
// ✅ CORRECT — object carries its own type
class UploadFile {
constructor(
readonly type: FileType,
readonly content: Buffer,
readonly originalFileName: string,
) {}
}
TS-specific: Branded Types for Primitive Identifiers
Use branded types to make identifiers self-descriptive and prevent accidental swaps at compile-time:
type TenantId = string & { readonly __brand: 'TenantId' };
type LandlordId = string & { readonly __brand: 'LandlordId' };
// ❌ Compile-time error — cannot pass TenantId where LandlordId expected
function getReceipts(landlordId: LandlordId): Receipt[] { /* ... */ }
getReceipts(tenantId); // Type error
// Factory function
function toTenantId(id: string): TenantId {
return id as TenantId;
}
See also:
ts-conventionsfor more on branded types and type-safety patterns.
04 同仓库更多 Skills
vitest-test-conventions
ACTIVATE when writing Vitest tests, creating test files, using vi.fn()/vi.mock()/vi.spyOn(), or test factories. ACTIVATE for 'Vitest', 'it.each', 'NestJS testing module', 'supertest', 'vi.fn', 'vi.mock', 'spyOn'. Provides Vitest/NestJS-specific testing patterns; cross-language testing principles (DAMP, AAA, spy over mock, what NOT to test) live in craft:testing-principles. DO NOT use for: node:test files (a file importing node:test) or a project whose tests run under node --test (see node-test:node-test-conventions), TDD workflow/iterations (see vitest:vitest-tdd-workflow), PHP/PHPUnit tests (see phpunit:php-test-conventions).
vitest-tdd-workflow
ACTIVATE when building new features, endpoints, or user stories using TDD with Vitest in a TypeScript/NestJS context. ACTIVATE for '/feature-tdd-dev', 'TDD', 'red-green-refactor' in TS context. Provides Vitest/NestJS-specific TDD examples and commands; cross-language TDD workflow (cross-layer iterations, RED-GREEN-REFACTOR, working app, bug-fix-first-test, mocks-hiding-bugs) lives in craft:tdd-workflow-principles. DO NOT use for: node:test files (a file importing node:test) or a project whose tests run under node --test (see node-test:node-test-conventions), test writing conventions (see vitest:vitest-test-conventions), PHP TDD (see phpunit:php-tdd-workflow).
ts-refactoring
ACTIVATE when refactoring TypeScript code. ACTIVATE for 'refactor', 'extract', 'redesign', 'simplify', or 'clean up' in a TypeScript / Node / NestJS context. This skill provides TS-specific examples for the cross-language refactoring rules defined in craft:refactoring-principles. DO NOT use for: writing new features from scratch, general OOP patterns (see ts-oop).
ts-ports-adapters
ACTIVATE when a TypeScript codebase with no dependency-injection container has to declare a port and wire its adapter — a repository, a clock, an id generator, an event dispatcher the domain depends on. ACTIVATE for 'port', 'adapter', 'SPI', 'dependency inversion' or 'composition root' in a codebase that wires its dependencies by hand. Covers: the port as a type of functions in the domain layer, the adapter as an object literal annotated with it, the handler that takes its dependencies first and its data second, the composition root in the infrastructure layer, and the in-memory stub that substitutes for the adapter in a unit test. DO NOT use for: NestJS, or any container-based DI with decorators and tokens (see nest-ddd-conventions), aggregate operations and smart constructors (see ddd-ts-fp), the Result type itself (see ts-functional).
ts-layer-boundaries
ACTIVATE when wiring a Worker entry point, a Router, a controller, or a domain event consumer in a TypeScript CQRS codebase, or when deciding where a port belongs. ACTIVATE for 'composition root', 'layer boundary', 'ESLint zones', 'domain must not import infrastructure'. Covers: the *Worker.ts composition root, the curried consumer, Router wiring of command endpoints, the five-gesture controller, the domain-to-infrastructure import boundary enforced by ESLint, infrastructure-only ports, in-memory doubles for every port, the read-can-reach-write asymmetry, and read endpoints going through a domain handler. DO NOT use for: the port's own shape and adapter binding (see ts-ports-adapters), the Result type (see ts-functional), domain event structure (see ts-ddd-events).
ts-functional
ACTIVATE when writing functional-style TypeScript: pipe, compose, currying, Result types, railway-oriented programming, or AsyncResult. ACTIVATE for 'pipe', 'Result type', 'railway', 'functional', 'chain'. Covers: type-safe pipe implementation, currying patterns, generic Result type, railway-oriented error handling, AsyncResult for async pipelines. DO NOT use for: DDD aggregate modeling (see ddd-ts-fp), OOP patterns (see ts-oop), imperative code.