typescript-coding-standards
Use when writing, refactoring, or reviewing TypeScript code that needs strong domain modeling, typed errors, schema parsing, safe adapters, test seams, or maintainable module boundaries.
- License 许可
- MIT
- 最近更新
- 2026-08-20
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- typescript-coding-standards
- tags
- [ "typescript", "code-quality", "architecture", "testing" ]
- tools
- []
- version
- 1.0.0
- agent_types
- [ "planner", "worker", "reviewer" ]
- description
- Use when writing, refactoring, or reviewing TypeScript code that needs strong domain modeling, typed errors, schema parsing, safe adapters, test seams, or maintainable module boundaries.
- dependencies
- []
03 SKILL.md 正文
TypeScript Coding Standards
Iron Laws
<EXTREMELY-IMPORTANT>
- No
any. Branded primitives, schema boundaries,unknown+ narrow. - Errors as data. Use typed domain errors,
Result<T, E>, orEffect<T, E>according to project conventions; do not force a library or use untyped throws for recoverable failures. - Pure core, effects at edges. Business logic takes inputs, returns values.
- Types describe the domain.
UserIdnotstring. - Test seams over mocking. Inject dependencies as values. </EXTREMELY-IMPORTANT>
Domain Modeling
// Branded primitives (no runtime cost)
type UserId = string & { readonly __brand: "UserId" }
const UserId = (s: string): UserId => s as UserId
// Discriminated unions
type RequestState<T> =
| { kind: "idle" }
| { kind: "loading" }
| { kind: "success"; data: T }
| { kind: "error"; error: AppError }
Use kind for discriminants (not type — collides with TS).
Schema Boundaries
Validate untrusted input at the edge. Inside, trust the types.
const input = Schema.decodeUnknownSync(UserSchema)(req.body)
// Now `input` is `User`, not `unknown`
Never let req.body, JSON.parse, process.env, or query strings reach the core. Decode at the boundary.
Error Modeling
Choose the error shape already used by the project or dependency boundary. Typed domain errors, Result, and Effect are valid options; preserve the public contract and avoid forcing a new error library for a local fix.
class UserNotFound extends Error {
readonly _tag = "UserNotFound" as const
constructor(readonly userId: UserId) { super(`User ${userId} not found`) }
}
type GetUser = (id: UserId) => Effect.Effect<User, UserNotFound | DbError>
The return type is the contract. Handlers switch on _tag.
Adapters
External systems get an adapter. Adapter implements a domain interface, hides the external API.
interface UserRepo {
findById: (id: UserId) => Effect.Effect<User, UserNotFound | DbError>
}
class PostgresUserRepo implements UserRepo {
findById = (id) => Effect.tryPromise({
try: () => pg.query("SELECT * FROM users WHERE id = $1", [id]),
catch: (e) => toDbError(e)
})
}
Business code depends on UserRepo, not pg. Tests use in-memory UserRepo.
Testing
Test pure core with input/output cases. Test impure edges through injected ports and deterministic fakes; assert boundary interactions. Mock unavoidable systems.
Module Boundaries
- One concern per module. Name after the concept, not the file type.
- Give public concepts the shortest name that greps uniquely; include a domain term and keep one concept, one spelling.
- Put a definition comment only where it states a hidden constraint or natural-language search phrase; do not restate the type.
- Public API: explicit exports. Internal: not exported or in
internal/. - No circular deps. If A imports B, B does not import A.
- Index files are minimal — only the public surface.
Red Flags
any in production; untyped JSON.parse; try/catch around await; Date.now() in logic; console.log left; data: any; circular imports; tests that don't test.
Anti-Patterns
"Just a string" (no branded type); "errors are exceptions"; "types later"; "test with mock" (test seam); "any to unblock"; "utils.ts".
04 同仓库更多 Skills
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment - applies TDD to process documentation by testing with subagents before writing, iterating until bulletproof against rationalization. Includes complete pressure testing methodology.
verification-before-completion
Use when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always
test-driven-development
Use when implementing a feature, bugfix, or behavior-changing refactor — require a failing behavior test before production code, then make the smallest change that passes.
source-driven-development
Use when unfamiliar libraries, dependency internals, external APIs, framework behavior, migrations, or current ecosystem guidance require official documentation, version-matched source, read the tests, and cited evidence.
security-and-hardening
Use when auditing security, implementing authentication or authorization, handling secrets, or validating data across external trust boundaries.
prototype
Use when the user wants to prototype, sanity-check a data model or state machine, mock up a UI, explore design options, or says "prototype this", "let me play with it", "try a few designs".