objectTypeDefinitions
Prefer interface declarations over type aliases for object types.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
TypeScript allows describing an object’s shape with either an interface declaration or a type alias of an object literal type.
Interfaces support declaration merging, can be extended with the extends keyword, and can be implemented by classes.
TypeScript’s error messages and hover information also tend to show an interface’s name directly, where a type alias may be expanded into its full object type.
This rule reports type aliases whose type is an object literal type, such as type Box = { value: string }.
Type aliases of other types, such as unions, intersections, and mapped types, are not reported because they can’t be written as interfaces.
Examples
Section titled “Examples”type Box = { value: string };type Point = { x: number; y: number;};interface Box { value: string;}interface Point { x: number; y: number;}type Status = "error" | "success";type Named<T> = T & { name: string };Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project has a convention of declaring object types with type aliases, such as to prevent declaration merging from adding properties to a type in other files, this rule will report each of those declarations.
You might prefer to disable this rule if converting them is not worth the churn in a large existing codebase.
Further Reading
Section titled “Further Reading”- TypeScript Handbook: Differences Between Type Aliases and Interfaces
- TypeScript Handbook: Declaration Merging
