json-schema-to-typescript
json-schema-to-typescript copied to clipboard
fix(optimizer): simplify redundant intersections in allOf schemas
Problem
When a JSON Schema uses allOf with $ref that overlap, the generated TypeScript contains redundant intersections and duplicate properties.
Example from the tsconfig.json schema:
export type CompilerOptions = {
types?: (string | null)[] | null;
target?: string | null;
[k: string]: unknown;
} & ({
types?: (string | null)[] | null; // Duplicate
target?: string | null; // Duplicate
[k: string]: unknown;
} | null);
This happens when:
- A schema definition is
$ref’d multiple times - One
$refwraps the base in anallOfwith extra constraints - The parser generates multiple AST nodes with the same name but different shapes
Before
export type CompilerOptions = {...} & ({...} | null);
Changes
-
Optimizer (
optimizer.ts)- Simplifies
A & (A | null)→A | null - Handles both
(A & (A | null))and(A | null) & A - Deduplicates intersections like
A & B & A & B → A & B
- Simplifies
-
Generator (
generator.ts)- Tracks generated type names with a
Set - Skips duplicate
export typedeclarations
- Tracks generated type names with a
After
export type CompilerOptions = {...} | null;