Skip to content

typescript/no-empty-object-type Restriction

What it does

To avoid confusion around the {} type allowing any non-nullish value, this rule bans usage of the {} type. That includes interfaces and object type aliases with no fields.

Why is this bad?

The {}, or "empty object" type in TypeScript is a common source of confusion for developers unfamiliar with TypeScript's structural typing. {} represents any non-nullish value, including literals like 0 and "". Often, developers writing {} actually mean either:

  • object: representing any object value
  • unknown: representing any value at all, including null and undefined In other words, the "empty object" type {}` really means "any value that is defined". That includes arrays, class instances, functions, and primitives such as string and symbol.

Note that this rule does not report on:

  • {} as a type constituent in an intersection type (e.g. types like TypeScript's built-in type NonNullable<T> = T & {}), as this can be useful in type system operations.
  • Interfaces that extend from multiple other interfaces.

Examples

Examples of incorrect code for this rule:

ts
let anyObject: {};
let anyValue: {};
interface AnyObjectA {}
interface AnyValueA {}
type AnyObjectB = {};
type AnyValueB = {};

Examples of correct code for this rule:

ts
let anyObject: object;
let anyValue: unknown;
type AnyObjectA = object;
type AnyValueA = unknown;
type AnyObjectB = object;
type AnyValueB = unknown;
let objectWith: { property: boolean };
interface InterfaceWith {
  property: boolean;
}
type TypeWith = { property: boolean };

Configuration

This rule accepts a configuration object with the following properties:

allowInterfaces

type: "never" | "always" | "with-single-extends"

Whether to allow empty interfaces.

Allowed values are:

  • 'always': to always allow interfaces with no fields
  • 'never' (default): to never allow interfaces with no fields
  • 'with-single-extends': to allow empty interfaces that extend from a single base interface

Examples of correct code for this rule with { allowInterfaces: 'with-single-extends' }:

ts
interface Base {
  value: boolean;
}
interface Derived extends Base {}

allowObjectTypes

type: "never" | "always"

Whether to allow empty object type literals.

Allowed values are:

  • 'always': to always allow object type literals with no fields
  • 'never' (default): to never allow object type literals with no fields

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny typescript/no-empty-object-type
json
{
  "rules": {
    "typescript/no-empty-object-type": "error"
  }
}

References

Released under the MIT License.