interface or type
For a plain object shape interface and type are almost interchangeable, and the choice matters in four places. Type also names unions and tuples and implicitly has an index signature, while interface allows declaration merging (how fields are added to Window) and extends, which rejects conflicting fields, whereas an & intersection quietly gives never. In practice: interface for models and classes, type for the rest.
TypeScript · Objects
You add a field to Window with type and the compiler says "Duplicate identifier". Why does interface allow it?
Tests often add their own globals to the page (an app-ready flag), and API models are described sometimes with interface, sometimes with type. For a plain object shape there is almost no difference, so people choose at random. Still, there are four places where the choice changes what the compiler does. The lesson covers them and gives a rule that does not need rechecking every time.
How it works and why. TypeScript types structurally: two types are compatible when their shapes match, not their names. That is why interface and type are interchangeable for an object. But they are the same only for a plain object shape. type names any type: a union, a tuple, a primitive, a function, a mapped or conditional type. interface describes only the shape of an object (and of a call or a constructor), but its declaration is open: two interfaces with the same name merge into one (declaration merging). That is how libraries let you extend their types, and how you add fields to Window or process.env. type is closed: a second declaration with the same name is an error.
// global.d.ts: add a field to the built-in Window
declare global {
interface Window {
appReady?: boolean;
}
}
export {};
// in a test
await page.waitForFunction(() => window.appReady === true);
The second difference is how they extend. An interface uses extends, a type uses the intersection &. They look alike but behave differently when fields conflict, and the example below shows it.
Example: conflicting fields. There is a base model, and a new one extends it with a different type of the id field (someone moved to numeric ids).
One conflict, two behaviours
type + intersection
No error at the declaration. The field becomes never, and the failure shows up later, in every test that uses it.
interface extends
The compiler stops at the declaration and names both types. The mismatch is visible before the first test.
type Base = { id: string };
type Order = Base & { id: number };
// no error: id is now string & number, i.e. never
const o: Order = { id: 1 };
// the error is here, at every use, not at the declaration
Why. An intersection does not check consistency, it just computes the field type as string & number, i.e. never: a value that does not exist. The error shows up where something is put into it. extends demands that the new declaration be a subtype of the base one and refuses at once:
interface Base { id: string }
interface Order extends Base { id: number }
// an error right at the declaration:
// Interface 'Order' incorrectly extends interface 'Base'
The third difference is less obvious and bites in test utilities. A type alias of an object literal implicitly gets an index signature, an interface does not. So a value of an interface cannot be passed to a function taking Record<string, unknown>, while a type can:
interface Account { id: string; iban: string }
type Transfer = { from: string; to: string };
function logRow(row: Record<string, unknown>) {
console.table(row);
}
declare const acc: Account;
declare const tr: Transfer;
logRow(tr); // ok
logRow(acc); // error: index signature is missing
How to choose. That leaves four questions: do you need a union or a tuple (then type), do you extend someone else's type (then interface with merging), is the value passed to code that expects an index signature (then type), is there a hierarchy of models (then interface extends gives early errors). In addition, the official compiler performance advice prefers extends over intersections for big hierarchies, since the compiler caches the result.
What to default to
Models and contracts
API objects, classes (implements), extending foreign types, hierarchies.
Everything that is not an object
Unions, tuples, literals, utility types, values for Record<string, unknown>.
Limits and alternatives. Neither option checks data at runtime: both vanish after transpilation. So for API responses a schema (zod, ajv) confirms the shape, and the type is derived from it. Another path: implements works with both. To stop arguments in review, the style is enforced with the ESLint rule @typescript-eslint/consistent-type-definitions: it requires one syntax (interface by default) for all object types, so exceptions such as models for Record<string, unknown> get an eslint-disable comment.
Common mistakes.
- Describing objects with an intersection instead of
extends: a field conflict is invisible, the field silently becomesnever. - Trying to extend a global type with
type: aninterfaceanddeclare globalin a module file are needed. - Accidentally declaring two
interfaces with the same name in the global scope (files withoutimport/export, ordeclare globalblocks): they merge and the fields of one appear in the other. In module files eachinterfaceis separate. - Mixing styles with no rule: time is wasted in review and the IDE hints become inconsistent.
Check yourself
Two global .d.ts files of a test utility library declare interface Config with their own fields, and code in a third file suddenly sees all the fields. A bug or a feature?
Answer
This is declaration merging: interfaces with the same name in a shared scope merge. It is needed for extending a library type, and accidental otherwise. To avoid it, use different names or type, which refuses the duplicate.
Why does log(row: Record<string, unknown>) accept a value of type Transfer = {...} but reject an interface Account value with the same fields?
Answer
For aliases of object literals TS infers an implicit index signature, for interfaces it does not, because they can be extended by merging and the new fields may have another type. The fix: type for such models or an explicit index signature.
type can do unions, tuples and primitives, while interface can do merging and extends. Middle/Senior: The choice has consequences: on a field conflict extends fails at the declaration and an intersection silently gives never; an interface has no implicit index signature; merging is needed to extend global types. So I use interface for models and classes, type for the rest, and enforce the default style with a linter.