Skip to content

fix: resolve TypeScript built-in type name collisions in CRD imports#3801

Open
mateo-moon wants to merge 1 commit intocdk8s-team:2.xfrom
mateo-moon:fix/builtin-type-name-collision
Open

fix: resolve TypeScript built-in type name collisions in CRD imports#3801
mateo-moon wants to merge 1 commit intocdk8s-team:2.xfrom
mateo-moon:fix/builtin-type-name-collision

Conversation

@mateo-moon
Copy link

@mateo-moon mateo-moon commented Feb 3, 2026

Summary

Fixes #3800

When a CRD's kind name matches a TypeScript built-in utility type (e.g., Record, Map, Set), the generated code creates a class that shadows the built-in type. This causes TypeScript compilation errors when the generated toJson_* functions use types like Record<string, any>.

Changes

Added a post-processing step in src/import/crd.ts that:

  1. Maintains a list of TypeScript built-in utility types that may conflict
  2. Detects when a CRD kind matches one of these types
  3. Adds a type alias (e.g., type JsonRecord<K extends keyof any, T> = { [P in K]: T };) at the beginning of the generated file
  4. Replaces usages of the built-in type with the alias (e.g., Record<string, any>JsonRecord<string, any>)

Example

For Cloudflare's DNS Record CRD, the generated code now includes:

// Type alias to avoid collision with the Record class defined below
type JsonRecord<K extends keyof any, T> = { [P in K]: T };

export class Record extends ApiObject {
  // ...
}

export function toJson_RecordProps(obj: RecordProps | undefined): JsonRecord<string, any> | undefined {
  // ...
}

Test Plan

  • Verified TypeScript compilation passes
  • Manual testing with Cloudflare DNS Record CRD
  • Consider adding unit tests for the fixBuiltinTypeConflicts function

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

auto-merge was automatically disabled February 3, 2026 11:48

Head branch was pushed to by a user without write access

@mateo-moon mateo-moon force-pushed the fix/builtin-type-name-collision branch from f585e0b to c5b25e3 Compare February 3, 2026 11:48
When a CRD's kind name matches a TypeScript built-in utility type (e.g.,
"Record", "Map", "Set"), the generated code creates a class that shadows
the built-in type. This causes TypeScript compilation errors when the
generated toJson functions use types like `Record<string, any>`.

This fix adds a post-processing step that:
1. Detects when a CRD kind matches a TypeScript built-in type
2. Adds a type alias (e.g., `JsonRecord<K, T>`) to preserve the built-in
3. Replaces usages of the built-in type with the alias

Example: Cloudflare's DNS `Record` CRD would generate a `Record` class,
and `Record<string, any>` usages are replaced with `JsonRecord<string, any>`.

Signed-off-by: OP <op@cicd.ninja>
@mateo-moon mateo-moon force-pushed the fix/builtin-type-name-collision branch from c5b25e3 to 3cdaf99 Compare February 3, 2026 11:52
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.

CRD import fails when kind name matches TypeScript built-in type (e.g., Record)

1 participant