wasAsked.

What is the difference between interface and type in TypeScript?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · TypeScript

Community answers

Posted by wasAsked editorial collection

Both describe the shape of data in TypeScript, and for plain object shapes they are often interchangeable. The differences matter in specific situations. Basic usage interface User { id: number; name: string; email?: string; } type UserType = { id: number; name: string; email?: string; }; Both can describe objects, functions, optional and readonly properties, and both can be implemented by classes. Key differences 1. What they can describe - type can name any type: unions, intersections, primitives, tuples, function types, mapped and conditional types. type Status = 'pending' | 'paid' | 'shipped'; // union — only possible with type type Point = [number, number]; // tuple type Id = string | number; type ReadonlyUser = Readonly<User>; // derived with utility types - interface describes object shapes (and function or constructor signatures within objects). 2. Extending - interface uses extends: interface Employee extends User { department: string; } - type uses intersections: type EmployeeType = UserType & { department: string }; - Interface extension reports conflicts as errors, while intersections of incompatible properties can produce never types that are harder to diagnose. 3. Declaration merging - Interfaces with the same name in the same scope merge automatically: interface Window { appVersion: string; } // adds a property to the global Window type - Types cannot be re-declared. Declaration merging is useful for augmenting library or global types (Express Request, Window) and is why library authors often expose interfaces. 4. Implements - Classes can implement both interfaces and object types; they cannot implement a union type. 5. Error messages and performance - Interfaces often produce clearer error messages and can be cached more efficiently by the compiler in large codebases with many extended types. 6. Advanced type programming - Mapped types and conditional types require type aliases: type Nullable<T> = { [K in keyof T]: T[K] | null }; type ApiResponse<T> = T extends Error ? { ok: false; error: T } : { ok: true; data: T }; Which to use - Many teams use interface for object shapes and public contracts (especially those meant to be extended or augmented), and type for unions, tuples, function types and composed or computed types. - The TypeScript documentation suggests using interface until you need features of type. - Consistency within the codebase matters more than the choice itself. Example combining both interface Product { id: string; name: string; price: number; } type ProductUpdate = Partial<Omit<Product, 'id'>>; type ApiResult<T> = { success: true; data: T } | { success: false; error: string }; Interview summary: "Both describe object shapes. Interfaces support declaration merging and clean extension and are great for object contracts. Type aliases can represent anything — unions, tuples, primitives, mapped and conditional types. Use interfaces for extensible object shapes and types for everything else."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics