Skip to main content

prefer-type-fest-json-object

Require TypeFest JsonObject over equivalent explicit Record<string, JsonValue> aliases.

Targeted pattern scopeโ€‹

This rule targets direct Record<string, JsonValue> aliases used as JSON object contracts.

  • Record<string, JsonValue>

Broader record aliases and structurally different object constraints are intentionally left unchanged.

What this rule reportsโ€‹

This rule reports Record<string, JsonValue> aliases that should use JsonObject.

  • Record<string, JsonValue>

Why this rule existsโ€‹

JsonObject communicates intent directly and avoids repeating verbose JSON object alias patterns.

โŒ Incorrectโ€‹

type Payload = Record<string, JsonValue>;

โœ… Correctโ€‹

type Payload = JsonObject;

Behavior and migration notesโ€‹

  • JsonObject is equivalent in intent to Record<string, JsonValue> for JSON object contracts.
  • Standardize on JsonObject for shared payload/config types to avoid repeating low-level dictionary syntax.
  • Keep runtime validation separate; this rule only normalizes compile-time type naming.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

type Payload = Record<string, JsonValue>;

โœ… Correct โ€” Additional exampleโ€‹

type Payload = JsonObject;

โœ… Correct โ€” Repository-wide usageโ€‹

type EventAttributes = JsonObject;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-json-object": "error",
},
},
];

When not to use itโ€‹

Disable this rule if a published API requires preserving existing alias names.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/json-value.d.ts

/**
Matches a JSON object.

This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { โ€ฆ }`.

@category JSON
*/

Rule catalog ID: R044

Further readingโ€‹

Adoption resourcesโ€‹