Skip to content

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.

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 ways

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 = number

The 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 never

extends 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.

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:

src/env.d.ts
declare module "bun" {
interface Env {
QUEUE_URL?: string
LOG_LEVEL?: "debug" | "info" | "warn" | "error"
}
}
const level = process.env.LOG_LEVEL // "debug" | "info" | "warn" | "error" | undefined

Merging 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.

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.

  • interface for object shapes that are named, extended or implemented: contracts like Sink, and anything other code augments.
  • type for 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.

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, type and interface are nearly interchangeable.
  • Only type can name unions, tuples, literals and computed types.
  • interface ... extends reports conflicts where they happen. & turns them into never.
  • Interfaces merge, which is how you augment library types such as Bun’s Env.
  • implements checks a class against a contract and doesn’t change its type.