/typescript:ts-conventionsfrom typescript
Enforces TypeScript conventions: strict mode, no-any, type over interface, branded types, discriminated unions, satisfies operator, and utility types. Activates when writing types, generics, or tsconfig.
Ships in typescript. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
strict: true is mandatory in tsconfig.json: without it, the whole convention set below is unenforceable — no any, discriminated unions, branded types all rely on the compiler actually checking null, implicit any, and type narrowing. No exceptions.
{
"compilerOptions": {
"strict": true,
// Array/object index access returns `T | undefined` instead of `T`,
// so a missing key fails to compile instead of crashing at runtime.
"noUncheckedIndexedAccess": true,
// An optional property (`foo?: string`) can no longer be set to
// `undefined` explicitly — it must be omitted, catching typos like
// `{ foo: undefined }` where `foo` was meant to be left out.
"exactOptionalPropertyTypes": true
}
}
strict: true is set once, at the repo root, and no package's own tsconfig.json may set it back to false to relax the check for that package alone.
Cost: a package tsconfig.json that relaxes strict silently loses noUncheckedIndexedAccess and exactOptionalPropertyTypes along with it, so a missing array index compiles clean inside that package and only crashes once another package imports it.
any — Use unknown + Narrowing// ❌ AVOID - any bypasses all type checking
function parse(input: any): string {
return input.name; // No error, but crashes if input has no name
}
// ✅ CORRECT - unknown forces explicit narrowing
function parse(input: unknown): string {
if (typeof input === 'object' && input !== null && 'name' in input) {
return String(input.name);
}
throw new Error('Invalid input');
}
unknown replaces any only at a boundary that receives untyped data — a parsed HTTP body, JSON.parse, a third-party callback — never inside code the domain already typed.
Cost: an any that survives past the parse boundary propagates through every function it touches, so the narrowing done three call sites downstream stops meaning anything.
typeof import(...) — Use import type// ❌ AVOID - Inline typeof import is unreadable and bypasses proper imports
const service = new MyService(
mock as unknown as InstanceType<
typeof import('../path/to/repository').Repository
>,
);
// ✅ CORRECT - Import the type at the top of the file
import type { Repository } from '../path/to/repository';
const service = new MyService(
mock as unknown as Repository,
);
import type applies to every import kept only for its type, not just the typeof import(...) pattern shown above.
Cost: a value import kept only for its type still pulls the whole module into the runtime bundle — the compiler considers it used, so tree-shaking cannot remove it.
enum — Use as const// ❌ AVOID - TypeScript enums have quirks (reverse mapping, runtime code)
enum Status {
Active = 'active',
Inactive = 'inactive',
}
// ✅ CORRECT - as const + type union
const STATUS = {
Active: 'active',
Inactive: 'inactive',
} as const;
type Status = (typeof STATUS)[keyof typeof STATUS]; // 'active' | 'inactive'
as const serves exactly two purposes: freezing a closed set to derive a union, and pinning a module constant to its literal type.
A closed set is written as a triplet in a single file: the as const object, the derived union, and the predicate.
const RECEIPT_STATUS = {
Draft: 'draft',
Sent: 'sent',
} as const;
type ReceiptStatus = (typeof RECEIPT_STATUS)[keyof typeof RECEIPT_STATUS];
const isReceiptStatus = (value: string): value is ReceiptStatus =>
Object.values(RECEIPT_STATUS).includes(value as ReceiptStatus);
type vs interfaceUse type by default. Use interface only when declaration merging or extends is needed (e.g., for NestJS class-based DI). The type-over-interface convention is enforced by ESLint's naming-convention rule, not by code review.
// ✅ type — for most cases
type Receipt = {
id: string;
amount: number;
period: Period;
};
// ✅ interface — when merging or extending is needed
interface CreateTenantDto {
email: string;
firstName: string;
}
interface UpdateTenantDto extends Partial<CreateTenantDto> {
id: string;
}
Model exclusive states explicitly — no impossible combinations:
// ❌ AVOID - Both fields optional = 4 possible states, only 2 are valid
type Receipt = {
data?: ReceiptData;
error?: string;
};
// ✅ CORRECT - Discriminated union = only valid states
type Receipt =
| { tag: 'fetched'; data: ReceiptData }
| { tag: 'not-fetched'; error: string };
function isFetched(receipt: Receipt): receipt is Extract<Receipt, { tag: 'fetched' }> {
return receipt.tag === 'fetched';
}
function handle(receipt: Receipt) {
if (isFetched(receipt)) {
console.log(receipt.data); // TypeScript knows data exists
} else {
console.error(receipt.error); // TypeScript knows error exists
}
}
The discriminant field is always named tag, and callers use isX predicates instead of comparing tag directly.
The
Result<T, E>discriminated union for fallible operations is owned byts-functional— do not redefine it here.
satisfies OperatorUse satisfies to validate a value matches a type without widening it:
type RouteConfig = Record<string, { path: string; auth: boolean }>;
// ❌ Type annotation widens — loses literal types
const routes: RouteConfig = {
home: { path: '/', auth: false },
dashboard: { path: '/dashboard', auth: true },
};
routes.home.path; // type: string (widened)
// ✅ satisfies — validates AND preserves literal types
const routes = {
home: { path: '/', auth: false },
dashboard: { path: '/dashboard', auth: true },
} satisfies RouteConfig;
routes.home.path; // type: '/' (preserved)
satisfies checks a rendered or collected object literal's shape without widening it; it is never used to validate an untyped value.
Prevent accidental swaps of primitive types that represent different concepts. A brand adds no data, so it can only ever be asserted — which is why the assertion is written once, in the kernel, and never in the domain:
// kernel/brand.ts — the one place in the codebase that asserts a brand
declare const brand: unique symbol;
export type Brand<T, B extends string> = T & { readonly [brand]: B };
export const mint = <B extends string>() => <T>(value: T): Brand<T, B> => value as Brand<T, B>;
// domain — no `as` here, and nothing outside can forge one
type TenantId = Brand<string, 'TenantId'>;
type LandlordId = Brand<string, 'LandlordId'>;
const toTenantId = mint<'TenantId'>();
const toLandlordId = mint<'LandlordId'>();
// ❌ Compile error — cannot mix branded types
function getReceipts(landlordId: LandlordId): Receipt[] { ... }
getReceipts(toTenantId('abc')); // Type error!
The brand key is a unique symbol that is never exported, so no caller can hand-write { ...value, __brand: 'TenantId' } and forge the proof. A string-literal key such as string & { readonly __brand: 'TenantId' } is addressable, which makes passing through the factory optional — and a proof that is optional is not a proof.
as is tolerated only at infrastructure boundaries: external payloads, JSON.parse, SDK calls, empty accumulators; inside the domain it is debt.
See also:
ts-oop's "TS-specific: Branded Types for Primitive Identifiers" heading for branded types in value objects.
Pick, Omit, and Partial derive a DTO or a command shape from an aggregate; the aggregate itself is never expressed as a Partial.
Cost: an aggregate typed as Partial<Tenant> for a command payload lets a caller construct it missing the very fields the constructor was written to guarantee, moving the invariant back to code review.
// Pick specific properties
type TenantSummary = Pick<Tenant, 'id' | 'email' | 'firstName'>;
// Omit properties
type CreateTenant = Omit<Tenant, 'id' | 'createdAt' | 'updatedAt'>;
// Make all properties optional
type PartialTenant = Partial<Tenant>;
// Make all properties required
type RequiredTenant = Required<Tenant>;
// Read-only
type FrozenTenant = Readonly<Tenant>;
// Record
type TenantMap = Record<TenantId, Tenant>;
// ✅ CORRECT - Constrained generics
function findById<T extends { id: string }>(items: T[], id: string): T | undefined {
return items.find(item => item.id === id);
}
// ✅ CORRECT - Generic with default
type ApiResponse<T = unknown> = {
data: T;
meta: { timestamp: string };
};
An unconstrained <T> is reserved to structural containers such as ApiResponse above; a domain function's generic always carries a semantic constraint (extends { id: string }, extends DomainError, …).
Cost: an unconstrained <T> on a domain function accepts any shape at all, so a typo'd field name on the call site fails at runtime instead of at the call.
// ✅ Explicit return types for nullable
function findTenant(id: string): Tenant | null { ... }
// ✅ Non-null assertion only when truly guaranteed
const element = document.getElementById('root')!; // Only in entry points
// ❌ AVOID - Non-null assertion to silence compiler
const tenant = findTenant(id)!; // Use narrowing instead
// ✅ CORRECT - Narrowing
const tenant = findTenant(id);
if (tenant === null) {
throw new NotFoundException('Tenant not found');
}
// tenant is now Tenant (narrowed)
Absence in a domain type is written as an explicit | null; ? is reserved for external payload shapes and partial update commands.
The non-null assertion ! is reserved to a framework entry point — application bootstrap, DI container resolution — never to a domain handler or service.
On the Command side, publish a named function-type alias after the handler's file and annotate the factory with it; on the Query side, let it infer.
The exported handler type alias lives in the handler's own file, never centralized in a shared types.ts barrel.
// createReceipt.ts
export type CreateReceiptHandler = (command: CreateReceiptCommand) => AsyncResult<Receipt, DomainError>;
const createReceipt: CreateReceiptHandler = (command) => { ... };
// findReceiptById.ts — Query, no alias, inferred return type
const findReceiptById = (query: FindReceiptByIdQuery) => { ... };
| Rule | Convention |
|---|---|
| Strict mode | strict: true, always |
No any | Use unknown + narrowing |
No typeof import(...) | Use import type at top of file |
No enum | Use as const + type union |
| type vs interface | type by default, interface for merging/extends |
| Exclusive states | Discriminated unions |
| Value validation | satisfies to preserve literal types |
| ID safety | Brand<T, B>, minted once in the kernel, never asserted in the domain |
| Subsets | Pick, Omit, Partial, Required |
| Nullability | Explicit ` |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
npx claudepluginhub fabiensalles/claude-marketplace --plugin typescript9 skills in this plugin
/ddd-ts-fpACTIVATE when modeling DDD aggregates, value objects and domain logic in TypeScript using functional patterns. ACTIVATE for 'aggregate', 'smart constructor', 'make*', 'validation pipeline', 'enrichment', 'domain handler' in TypeScript. Provides TypeScript-specific examples for cross-language functional DDD principles defined in craft:ddd-fp-principles. DO NOT use for: infrastructure code, general FP patterns (see ts-functional), OOP modeling (see ts-oop)./ts-code-conventionsACTIVATE whenever writing or modifying TypeScript code in src/. ACTIVATE for code review, formatting, or 'coding standards'. Provides TS-specific code style examples for the cross-language rules defined in craft:code-style-principles, plus TS-specific patterns (optional chaining, nullish coalescing, template literals, no truthy/falsy). DO NOT use for: TypeScript typing rules (see ts-conventions), test conventions, domain/infra layer boundaries (see ts-layer-boundaries)./ts-ddd-eventsACTIVATE when implementing domain events, event store, outbox pattern, or reliable event publishing in TypeScript. ACTIVATE for 'domain event', 'outbox', 'event store', 'event-driven', 'reliable publishing'. Covers: domain event structure and naming, event store (append-only), outbox pattern for reliable publishing (no dual-write), consumer pattern. DO NOT use for: aggregate modeling (see ddd-ts-fp), general async patterns, message queue configuration./ts-functionalACTIVATE 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./ts-layer-boundariesACTIVATE 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-oopACTIVATE 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).