dev3 ·
Both seem to describe object shapes. Is there a real difference, and which should I pick in a new project?
0
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
Both seem to describe object shapes. Is there a real difference, and which should I pick in a new project?
For plain object shapes they are almost interchangeable. Differences that matter:
interface Window { myAnalytics: Analytics }
extends, and error messages are often clearer.implements it).type ID = string | number;
type Pair = [string, number];
type Status = 'idle' | 'loading' | 'error';
type Keys = keyof User;
type Readonly2<T> = { readonly [K in keyof T]: T[K] };
&.interface for public object contracts and class shapes, particularly in libraries (users can extend it).type for unions, tuples, utility/mapped types and function types.type everywhere, others follow "interface until you need type".interface extends is cached by the compiler and can be faster than big intersections of type aliases in very large codebases.
Prefer string-literal unions over enum for simple cases; they have no runtime output:
const roles = ['admin', 'editor', 'viewer'] as const;
type Role = typeof roles[number];
Pitfall: using object or {} as a type accepts almost anything. Use Record<string, unknown> for dictionaries.