/typescript:ts-ports-adaptersfrom typescript
Guides implementing ports and adapters in TypeScript without a DI container. Covers port types, adapter object literals, dependency-first handlers, composition roots, and in-memory stubs for testing.
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.
Injection here is a function parameter and nothing else. There is no container to register an implementation with, and no decorator to mark one — so the seam has to be carried by the types and by where each piece is applied.
Result<T, E>andAsyncResult<T, E>arets-functional's; the aggregate operations a handler pipes areddd-ts-fp's. When a container is present, the same seam is declared with a token and a decorated class instead — that shape belongs tonest-ddd-conventions, not here.
// src/Command/Domain/SPI/ReceiptRepository.ts
export type ReceiptRepository = {
getById: (receiptId: string) => AsyncResult<Receipt, DomainError>;
save: (receipt: Receipt) => Promise<void>;
};
Each field is a property typed as an arrow function, not method shorthand.
Zero ports across the reference codebases declare a field with method shorthand. Method syntax is the
form a class implements, and it invites the container-shaped reflex of making the adapter a class;
an object literal cannot satisfy a contract it was never asked to extend.
A port with one operation needs no object around it:
// src/Command/Domain/SPI/EventDispatcher.ts
export type EventDispatcher = (domainEvent: DomainEvent) => Promise<void>;
| Rule | Convention |
|---|---|
| Shape | type whose fields are arrow-typed properties — never an interface of methods |
| Location | the domain layer: Domain/SPI/, or domain/ports/ |
| Naming | the role, never the technology — ReceiptRepository, not MongoReceiptRepository |
| Imports | domain types only; a port importing its driver has stopped being a port |
| Granularity | one port per collaborator, not one per feature — the handler names the ones it uses |
// src/Command/Infrastructure/MongoDB/Receipts/ReceiptRepository.ts
const getById = async (receiptId: string): AsyncResult<Receipt, DomainError> => {
const document = await receiptCollection().findOne<ReceiptDocument>({ _id: receiptId });
if (document === null) {
return failure(receiptNotFound(receiptId));
}
return success(toReceipt(document));
};
const save = async (receipt: Receipt): Promise<void> => {
await receiptCollection().replaceOne({ _id: receipt.id }, toDocument(receipt), { upsert: true });
};
const receiptRepository: ReceiptRepository = { getById, save };
export default receiptRepository;
The annotation is what binds the adapter to the port: drop a field or drift a signature and the
compiler refuses the object. Every adapter in the reference codebases carries this annotation, with
zero adapters left untyped. Use the annotation rather than satisfies — the port is exactly the
type the consumer wants, so there are no literal types worth preserving. No adapter in either
reference codebase is declared with satisfies in place of the port annotation.
// src/Command/Domain/Features/GenerateReceipt/GenerateReceiptHandler.ts
export type GenerateReceiptHandler = (command: GenerateReceiptCommand) => AsyncResult<Receipt, DomainError>;
export const generateReceiptHandler =
(receiptRepository: ReceiptRepository, eventDispatcher: EventDispatcher): GenerateReceiptHandler =>
async (command) => {
const receipt = await receiptRepository.getById(command.receiptId);
if (isFailure(receipt)) {
return receipt;
}
await receiptRepository.save(receipt.value);
await eventDispatcher(receiptGenerated(receipt.value));
return receipt;
};
The outer call takes every port and returns the named handler type; the inner call takes the
command. Every handler in the reference codebases follows this two-call shape, with zero handlers
taking their dependencies and their data in a single parameter list. Dependencies are resolved
once, at startup, and the inner function is what the rest of the
application holds — so nothing downstream ever needs to know what a ReceiptRepository really is.
Applying a handler to its adapters is an infrastructure act. Zero domain files in the reference codebases apply a handler to its adapters; every application happens in a controller, a worker, or a CLI entry point. It happens at the entry point that owns the process — a controller, a worker, a CLI — and never inside the domain, which must stay ignorant of which adapter it runs against.
// src/Command/Infrastructure/Http/Controllers/GenerateReceiptController.ts
import { generateReceiptHandler } from '@CommandDomain/Features/GenerateReceipt/GenerateReceiptHandler';
import receiptRepository from '@CommandInfrastructure/MongoDB/Receipts/ReceiptRepository';
import sqsEventDispatcher from '@CommandInfrastructure/SQS/EventDispatcher';
const generateReceipt = generateReceiptHandler(receiptRepository, sqsEventDispatcher);
const generateReceiptController = async (request: Request, response: Response): Promise<void> => {
const receipt = await generateReceipt(toGenerateReceiptCommand(request));
response.status(isFailure(receipt) ? UNPROCESSABLE_ENTITY : CREATED).send();
};
Declaring a port and then importing the concrete adapter everywhere is the failure mode this shape exists to prevent: it keeps the type and loses the seam. No consumer file in the reference codebases imports a concrete adapter while a port of the same name exists. If a consumer imports the adapter instead of receiving the port, the port is decoration.
The same call site takes an in-memory stub. Every port in the reference codebases has at least one test exercising this substitution. Nothing in the domain changes, and no mocking library is involved — the stub records what it was given and answers what the test needs.
// tests/Unit/Command/Domain/Features/generateReceiptHandler.spec.ts
const savedReceipts: Receipt[] = [];
const inMemoryReceiptRepository: ReceiptRepository = {
getById: async (receiptId) => success(aReceipt({ id: receiptId })),
save: async (receipt) => {
savedReceipts.push(receipt);
},
};
await generateReceiptHandler(inMemoryReceiptRepository, async () => {})(aGenerateReceiptCommand());
assert.deepStrictEqual(savedReceipts.length, 1);
Because the stub is an ordinary value, it doubles as the spy — asserting on savedReceipts reads
better than asserting on a recorded call. Zero tests in the reference codebases pair a hand-written
stub with a separate spy assertion. Which double to reach for, and what does not deserve a
test at all, is craft:testing-principles's subject, not this skill's.
A CQRS-split aggregate carries a homonymous write port and read port whose operations never overlap.
The two ports live in their own slice — Command/Domain/SPI/ReceiptRepository.ts writes,
Query/Domain/SPI/ReceiptRepository.ts reads — and neither one grows an operation that belongs to
the other:
// src/Command/Domain/SPI/ReceiptRepository.ts
export type ReceiptRepository = {
getById: (receiptId: string) => AsyncResult<Receipt, DomainError>;
save: (receipt: Receipt) => Promise<void>;
};
// src/Query/Domain/SPI/ReceiptRepository.ts
export type ReceiptRepository = {
findByCustomer: (customerId: string) => Promise<ReceiptView[]>;
};
A save on the read side, or a findByCustomer on the write side, is the two slices leaking into
each other through a shared name.
The adapter is a lowerCamel const annotated by its port and exported as the module's default.
export default receiptRepository; is not decoration on the earlier example — it is the shape
every adapter in the reference codebase takes, and the composition root imports that default,
never a named export scavenged from the module.
Only the clock, id generation, persistence, dispatch, and feature flags sit behind a port; everything else is reached directly. A pure computation — pricing a receipt, validating a command's shape, deriving a discount — never gets a port of its own; wrapping it behind one adds indirection a unit test never needed and a call site never wanted.
Every domain port lives under SPI/, with no port left outside it by convention. The two reference
codebases disagree here: one keeps all of its ports under Domain/SPI/, the other leaves several
of them scattered next to the features that use them. This skill takes the first codebase's side
and states it as the rule — a port declared outside SPI/ is a deviation to fix, not a second
valid layout to tolerate.
Each port keeps a hand-written double: InMemory for one that holds state, Stub for one that returns a deterministic sequence.
InMemoryReceiptRepository accumulates what it is given and answers from what it holds;
StubClock answers the same instant every time it is called. Neither double is generated by a
mocking library — both are ordinary values a test constructs by hand.
A port that only circulates between two infrastructure files has no reason to live in SPI. If
nothing in Domain/Features/ takes the port as a parameter, there is no seam to preserve — the
type is infrastructure talking to infrastructure, and belongs next to the adapters that use it,
not under SPI/.
| Element | Convention |
|---|---|
| Port | type of arrow-typed properties, in the domain |
| Port with one operation | a bare call signature, no wrapping object |
| Adapter | const x: Port = { … }, in the infrastructure |
| Binding | the type annotation — never a token, a decorator or a registration |
| Handler | (…ports) => (data), dependencies first |
| Composition | the infrastructure entry point, never the domain |
| Test double | an in-memory object of the same port, doubling as the spy |
| CQRS ports | homonymous write/read ports, disjoint operations |
| Adapter export | default export of a lowerCamel const |
| Port candidate | non-deterministic or I/O only — never a pure computation |
| Port location | SPI/, without exception |
| Hand-written double | InMemory if stateful, Stub if a deterministic sequence |
| Port consumer | the domain — never infrastructure alone |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
9 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-conventionsACTIVATE when writing TypeScript types, generics, branded types, discriminated unions, or configuring tsconfig. ACTIVATE for 'type vs interface', 'enum alternative', 'branded type', 'satisfies', 'strict mode'. Covers: strict mode policy, no-any/no-enum rules, type over interface, discriminated unions, satisfies operator, branded types for ID safety, utility types. DO NOT use for: code formatting (see ts-code-conventions), functional patterns (see ts-functional)./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).npx claudepluginhub fabiensalles/claude-marketplace --plugin typescript