type vs interface — which to choose?
Answer out loud or in your head first, then compare.
Answer
Both can describe the shape of an object. An interface supports extension and declaration merging, while a type can also name a union, a tuple or a primitive type.
TypeScript
interface User { id: string; name: string }
interface Admin extends User { permissions: string[] }
type Status = 'idle' | 'loading' | 'error';
type Named = { name: string };In practice: I follow the team's convention. There's no rule that one is always better.
Pitfall: a type describes and checks code before it runs. An interface on its own doesn't validate JSON from an API and doesn't exist as a runtime object.
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- B-P11 · How do type and interface differ in TypeScript? — JavaScript, TypeScript and Node.js