Adapter
An adapter converts one type into another so a foreign API can satisfy your interface. In TypeScript that conversion is a pair of functions (or a class) whose parameter and return types are the two shapes.
Detailed Explanation
You have TheirUser from a vendor and User in your app. You cannot change the vendor. You should not leak TheirUser inward. An adapter is function toUser(row: TheirUser): User and, if you send data back, function fromUser(user: User): TheirUser. Classes that implements UserRepo and call the vendor inside are adapters too.
The type rules are ordinary and easy to skip:
- The adapter's public type mentions only your types.
- The vendor type appears only in the adapter file (or a
vendor/module). - Extra vendor fields are dropped on purpose (
Pick/ explicit object literal), not passed through with spread if you do not want them leaking. - Missing vendor fields become
undefinedor a default, and the return type must say so.
Structural typing means a vendor object that already has id and email is assignable to User without an adapter. That is a trap. The adapter's job is often to stop extra fields and to rename (user_id → id). Write an explicit literal. Excess-property checks then apply. satisfies User on that literal is a good check.
Generic adapters (function adapt<T, U>(...)) only pay off when the mapping is mechanical (rename keys via a map type). Most adapters are one function per pair of types. A mapped type can describe the rename ({ [K in keyof Their]: ... }) but the runtime still needs a loop or a literal.
Adapters sit under facades: the facade is the API you wanted; the adapter is the translation to the API you have. They sit under repositories: the repo is the collection; the adapter is the row mapper.
Do not type the adapter as (row: any) => User. The whole point is that TheirUser is named, so a vendor field rename breaks this file, not every feature.
Code Examples
1. Explicit mapping — extra fields do not leak.
// 1. Explicit mapping — extra fields do not leak.
type TheirUser = { user_id: string; email: string; extra: string };
type User = { id: string; email: string };
export function toUser(row: TheirUser): User {
return { id: row.user_id, email: row.email };
}2. A class adapter that implements your interface.
// 2. A class adapter that implements your interface.
interface Clock { now(): Date }
class OffsetClock implements Clock {
constructor(private inner: Clock, private offsetMs: number) {}
now(): Date {
return new Date(this.inner.now().getTime() + this.offsetMs);
}
}3. satisfies to catch a missed rename.
// 3. satisfies to catch a missed rename.
const sample: TheirUser = { user_id: "1", email: "a@b.c", extra: "x" };
const user = toUser(sample) satisfies User;Common Gotchas
Assigning `TheirUser` directly to `User` because both have `email` hides the rename
if the vendor uses `user_id`, that assignability is a coincidence you should not use.
One adapter file should import the vendor type
everyone else imports `User`.
A generic "auto-mapper" will miss semantic changes
field-by-field is the reliable default.