emptyObjectTypes
Reports empty object type literals and empty interfaces that are often used incorrectly.
✅ This rule is included in the ts logical presets.
The {} (“empty object”) type in TypeScript is a common source of confusion.
It actually represents any non-nullish value, not an empty object.
This includes primitives like strings and numbers.
Similarly, an empty interface interface Foo {} is equivalent to {}.
If you want to represent any object, use object.
If you want any value, use unknown.
Examples
Section titled “Examples”type Empty = {};interface Empty {}let value: {};function foo(param: {}) {}type HasMembers = { foo: string };interface HasMembers { foo: string;}let value: object;let value: unknown;// Empty object in intersection types is allowed (useful for NonNullable)type NonNullable<T> = T & {};// Empty interface extending multiple interfaces is allowedinterface Combined extends Foo, Bar {}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your code commonly needs to represent the “any non-nullish value” type, this rule may not be for you. Projects that extensively use type operations such as conditional types and mapped types may benefit from disabling this rule.
Further Reading
Section titled “Further Reading”- TypeScript ESLint: Revamping the
ban-typesrule - Total TypeScript: The Empty Object Type in TypeScript
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.