Skip to content
Sarj Code Standards

prefer-dependency-interface

Annotate retained collaborators with their existing interface when the consumer uses only that contract.

Why

A concrete annotation can require private implementation identity even when a declared interface covers every consumed operation.

Fix

Use the existing interface on the injected parameter. Keep concrete annotations for implementation-specific capabilities.

Examples

Before — flagged The consumer uses only the interface
service.ts
interface Store {
read(): string;
}
class SqlStore implements Store {
private value = "ok";
read() {
return this.value;
}
}
class Service {
constructor(private readonly store: SqlStore) {}
run() {
return this.store.read();
}
}
After — preferred Accept another implementation
service.ts
interface Store {
read(): string;
}
class Service {
constructor(private readonly store: Store) {}
run() {
return this.store.read();
}
}