Object Types and InterfacesLesson 2.2
TypeScript interfaces vs type aliases what is the difference
interface declaration, type alias declaration, extending interfaces, intersection types, declaration merging, when to use each
interface vs type
Both describe object shapes, and for most use cases they are interchangeable:
interface User {
name: string;
age: number;
}
type UserType = {
name: string;
age: number;
};Key differences
Interfaces can be extended:
interface Animal { name: string; }
interface Dog extends Animal { breed: string; }Type aliases support unions:
type ID = string | number; // can't do this with interfaceInterfaces support declaration merging:
interface Window { myProp: string; }
interface Window { otherProp: number; }
// TypeScript merges both — useful for augmenting library typesWhich to use
Use interface for object shapes you will extend or that describe a public API contract. Use type for unions, intersections, mapped types, and utility types. When in doubt, interface for objects is the community convention.
