type vs interface
Relay writes log lines to a sink: stdout in production, an in-memory buffer in tests. That’s
one contract with several implementations, the case interface was made for. You’ve used both
type and interface already. For an object shape they’re nearly the same thing. This lesson is
about the places where they differ, so you can choose on purpose.
The same shape, two spellings
Section titled “The same shape, two spellings”interface SinkA { readonly name: string write(line: string): void}
type SinkB = { readonly name: string write(line: string): void}
const a: SinkA = { name: "stdout", write: (line) => console.log(line) }const b: SinkB = a // structurally identical, so assignable both waysWhat only type can do
Section titled “What only type can do”type names any type. interface only describes object shapes (including functions and
classes). Unions, tuples, literals and aliases of primitives need type:
type LogLevel = "debug" | "info" | "warn" | "error"type HostPort = readonly [host: string, port: number]type Source = { from: "env"; variable: string } | { from: "default" }type Port = numberThe type-level tools in M4 (mapped and conditional types) are type-only too.
Extending: extends reports conflicts, & hides them
Section titled “Extending: extends reports conflicts, & hides them”Both can build a shape from another one. They behave differently when the two disagree:
interface Sink { readonly name: string}
// @ts-expect-error: interface 'Broken' incorrectly extends interface 'Sink'interface Broken extends Sink { readonly name: number}
type Silent = Sink & { readonly name: number } // accepted: name is now neverextends checks compatibility where the new interface is declared. An intersection never fails
where it’s written, and the conflict shows up later as a never property, as you saw in
lesson 5. For shapes built from other shapes, interface ... extends gives the better error.
Declaration merging
Section titled “Declaration merging”Two interface declarations with the same name in the same scope merge into one. Two type
declarations with the same name are an error. Merging is how you add to a type owned by someone
else. Bun, for example, types process.env and Bun.env with an interface named Env that it
expects you to extend:
declare module "bun" { interface Env { QUEUE_URL?: string LOG_LEVEL?: "debug" | "info" | "warn" | "error" }}
const level = process.env.LOG_LEVEL // "debug" | "info" | "warn" | "error" | undefinedMerging also has a downside: an interface can be extended from anywhere, including by accident, if a second declaration with the same name is in scope.
implements
Section titled “implements”A class can promise to match an interface (or an object type). The class is checked against it
where it’s declared:
interface Sink { readonly name: string write(line: string): void}
class StdoutSink implements Sink { readonly name = "stdout" write(line: string): void { console.log(line) }}
// @ts-expect-error: property 'write' is missing in type 'NullSink'class NullSink implements Sink { readonly name = "null"}implements doesn’t change the class’s type. It only adds a check. Because of structural typing,
any object with name and write is a Sink, whether it was declared with implements or not.
Which one to use
Section titled “Which one to use”interfacefor object shapes that are named, extended or implemented: contracts likeSink, and anything other code augments.typefor everything else: unions, tuples, literals, function types, and type-level computations.
Consistency within a codebase matters more than the choice. The TypeScript handbook recommends
interface until you need a feature only type has.
Exercise
Section titled “Exercise”BufferedSink extends Sink with a capacity, and MemorySink implements it. The compiler finds
a bad extends and an incomplete class. The tests describe how the buffer behaves when it’s full.
- For object shapes,
typeandinterfaceare nearly interchangeable. - Only
typecan name unions, tuples, literals and computed types. interface ... extendsreports conflicts where they happen.&turns them intonever.- Interfaces merge, which is how you augment library types such as Bun’s
Env. implementschecks a class against a contract and doesn’t change its type.
Further reading
Section titled “Further reading”- Handbook: Everyday Types: the differences between type aliases and interfaces.
- Handbook: Declaration Merging: declaration merging.
- Bun docs: Environment Variables: the
Envinterface Bun lets you extend.