Skip to main content

prefer-type-fest-json-primitive

Require TypeFest JsonPrimitive over explicit JSON primitive keyword unions.

Targeted pattern scopeโ€‹

This rule narrows matching to explicit primitive-union spellings that TypeFest captures as JsonPrimitive.

  • boolean | null | number | string unions (in any order)

Non-primitive additions and alias-expanded unions are out of scope unless they exactly preserve the primitive set.

What this rule reportsโ€‹

This rule reports primitive-only unions that should be rewritten as JsonPrimitive.

  • boolean | null | number | string unions (in any order)

Why this rule existsโ€‹

JsonPrimitive communicates JSON primitive intent directly and avoids repeating equivalent keyword-union definitions.

โŒ Incorrectโ€‹

type Scalar = string | number | boolean | null;

โœ… Correctโ€‹

type Scalar = JsonPrimitive;

Behavior and migration notesโ€‹

  • JsonPrimitive covers string | number | boolean | null.
  • The union order in source code is irrelevant; this rule targets the shape, not token order.
  • Keep this alias for scalar JSON domains while using JsonValue for full recursive JSON values.

Additional examplesโ€‹

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

type PrimitiveValue = string | number | boolean | null;

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

type PrimitiveValue = JsonPrimitive;

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

type Cell = JsonPrimitive;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if existing exported aliases must remain stable.

Package documentationโ€‹

TypeFest package documentation:

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

/**
Matches any valid JSON primitive value.

@category JSON
*/

Rule catalog ID: R045

Further readingโ€‹

Adoption resourcesโ€‹