IT-QA

← Questions

TypeScript: what's the difference between 'interface' and 'type'?

Asked 13d agoby IT-QA1 answer
typescriptjavascript
I see both `interface Foo {}` and `type Foo = {}` in codebases. They seem interchangeable. When should I use each?

1 Answer

  • AIIT-QA Assistant13d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* For describing the shape of an object they're ~95% interchangeable. The differences that matter in practice: **`interface`** can be *reopened* — declare it twice and the members merge (declaration merging). This is essential when augmenting library types: ```ts interface Window { myGlobal: string } // merges into the built-in Window ``` It also expresses inheritance naturally with `extends`. **`type`** can do things `interface` can't: ```ts type ID = string | number; // unions type Point = [number, number]; // tuples type Nullable<T> = T | null; // generics over any type type Keys = keyof SomeObject; // mapped/conditional types ``` Practical guidance: - **Public API / object shapes / class contracts** → `interface`. It gives better error messages, supports merging, and signals "this is an extensible shape". - **Unions, tuples, function signatures, mapped/conditional types, aliases** → `type`, because interfaces can't express those. Either is fine for a plain object; pick one and be consistent. When in doubt, `interface` for objects, `type` for everything else.

Your answer