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 | stringunions (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 | stringunions (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โ
JsonPrimitivecoversstring | 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
JsonValuefor 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