Skip to main content

prefer-type-fest-json-value

Prefers TypeFest JsonObject for serialization-bound string-keyed record contracts.

Targeted pattern scopeโ€‹

This rule targets boundary-layer Record<string, unknown/any> payload aliases that are intended to represent JSON-compatible values.

  • Payload/context-like contract aliases using Record<string, unknown>/Record<string, any> in JSON boundary folders.

General-purpose records outside configured JSON boundaries are intentionally excluded.

What this rule reportsโ€‹

This rule reports boundary payload aliases that should use JsonObject.

  • Payload/context-like contract aliases using Record<string, unknown>/Record<string, any> in JSON boundary folders.

Why this rule existsโ€‹

Serialization boundaries should declare JSON-compatible intent directly so type safety and runtime assumptions stay aligned.

โŒ Incorrectโ€‹

type Payload = Record<string, unknown>;

โœ… Correctโ€‹

type Payload = JsonObject;

Behavior and migration notesโ€‹

  • JsonObject models JSON-compatible object payloads with string keys.
  • Use JsonObject when your boundary contract must stay object-shaped.
  • Replace open-ended Record<string, unknown>/any boundary contracts with JsonObject when schema intent is serialization.

Additional examplesโ€‹

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

type ConfigSnapshot = Record<string, unknown>;

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

type ConfigSnapshot = JsonObject;

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

type CacheEntry = JsonObject;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if boundary contracts intentionally allow non-JSON runtime values.

Package documentationโ€‹

TypeFest package documentation:

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

/**
Matches any valid JSON value.

@see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.

@category JSON
*/

Rule catalog ID: R046

Further readingโ€‹

Adoption resourcesโ€‹