Skip to content

fix(optimizer): simplify redundant intersections in allOf schemas#672

Open
privatenumber wants to merge 2 commits intobcherny:masterfrom
privatenumber:test/tsconfig-duplication-bug
Open

fix(optimizer): simplify redundant intersections in allOf schemas#672
privatenumber wants to merge 2 commits intobcherny:masterfrom
privatenumber:test/tsconfig-duplication-bug

Conversation

@privatenumber
Copy link
Copy Markdown

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:

  1. A schema definition is $ref’d multiple times
  2. One $ref wraps the base in an allOf with extra constraints
  3. The parser generates multiple AST nodes with the same name but different shapes

Before

export type CompilerOptions = {...} & ({...} | null);

Changes

  1. 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
  2. Generator (generator.ts)

    • Tracks generated type names with a Set
    • Skips duplicate export type declarations

After

export type CompilerOptions = {...} | null;

oneOf?: SchemaArray;␊
not?: CoreSchemaMetaSchema;␊
[k: string]: unknown;␊
} & (␊
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it was able to de-duplicate this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant