Skip to content
Sarj Code Standards

require-port-for-service

Require exported-class contracts and advise when an exported injected service's public methods are not covered by its declared ports.

Why

A declared port keeps consumers coupled to the service capability instead of its concrete implementation. An explicit contract names the intended public capability separately from implementation details. TypeScript already supports structural compatibility; this is an architecture policy, not a prerequisite for substitution.

Fix

Declare and implement an interface covering the service's public methods; for exported concrete classes, declare a focused interface or inherit from an intentional base contract.

Examples

Before — flagged Do not expose only the concrete service
src/service.ts
export class RequestHandler {
constructor(private readonly store: TaskStore) {}
handle(): void {
this.store.handle();
}
}
After — preferred Implement the service port
src/service.ts
interface Handler {
handle(): void;
}
export class RequestHandler implements Handler {
constructor(private readonly store: TaskStore) {}
handle(): void {
this.store.handle();
}
}

Formerly: require-interface-for-exported-class, require-interface-for-injected-service