Facade
A facade is a narrow type in front of a messy subsystem. Callers import one module and one interface. The implementations, overloads, and third-party types stay behind that door.
Detailed Explanation
Subsystems accumulate types: SDK clients, generated protobufs, four helper modules, a retry wrapper. A facade is a single exported type that describes what the rest of the app may do, plus a function that builds the concrete object.
In TypeScript the facade is often:
export interface Payments {
charge(input: ChargeInput): Promise<Charge>;
}
export function createPayments(config: PaymentsConfig): Payments {
// wires Stripe, logs, retries
}
ChargeInput and Charge are your types. The Stripe types stay in the implementation file. That is the whole gain: you can replace Stripe without touching feature modules, and you can fake Payments in tests because it is an interface.
A facade is not a barrel index.ts that re-exports fifty names. Re-exports do not narrow anything; they make the messy surface official. Export the small interface and the factory. Keep the rest unexported or in a internal/ path your tsconfig paths / exports do not expose.
Facades compose with DI: the facade interface is what you inject. They compose with adapters: the adapter translates one foreign type to yours; the facade is the name you give the translated API. An adapter is about one foreign shape. A facade is about hiding a cluster.
Type-only facades (export type { Payments }) plus a separate factory file work well with isolatedModules. Do not put implementation types on the exported interface "just in case." Every extra method is a promise you will keep for every caller.
The JavaScript facade page covers the structural idea. Here, the discipline is: export a small interface, map foreign types at the boundary, and let structural typing make fakes trivial.
Code Examples
1. Public types — no SDK types.
// 1. Public types — no SDK types.
export type ChargeInput = { cents: number; currency: "usd" | "eur" };
export type Charge = { id: string; cents: number };
export interface Payments {
charge(input: ChargeInput): Promise<Charge>;
}2. Factory hides the concrete client.
// 2. Factory hides the concrete client.
export function createPayments(apiKey: string): Payments {
return {
async charge(input) {
return { id: "ch_1", cents: input.cents };
},
};
}3. Feature code depends on the interface.
// 3. Feature code depends on the interface.
async function checkout(payments: Payments, cents: number): Promise<string> {
const charge = await payments.charge({ cents, currency: "usd" });
return charge.id;
}Common Gotchas
A barrel file is not a facade
if callers can import the SDK through you, they will.
Putting generated types on the public interface locks you to the vendor
map them.
The facade should be an interface or a narrow type alias
a concrete class as the export makes fakes extend it.
Growing the facade "one method for now" is how it becomes the subsystem
add a second facade when the domains split.