Skip to content

Conversation

@chris-mangrove
Copy link

Summary

  • Fixes incorrect self-reference detection for namespaced serializers
  • Prevents TypeScript error TS2440 (module imports itself)

Problem

When a namespaced serializer (e.g., Inventory::ItemSerializer) has a self-referential association (like a tree structure with parent/children), the generated TypeScript file incorrectly imported itself:

// Generated with bug - imports itself!
import { InventoryItem } from './InventoryItem';

export interface InventoryItem {
  id: number;
  parent: InventoryItem;
  children: InventoryItem[];
}

This caused TypeScript error TS2440: "Import declaration conflicts with local declaration."

Root Cause

The self_type_name method used a regex that only captured the last segment of the serializer name:

serializer.name.match(/(\w+::)?(\w+)(Serializer|Resource)/)[2]
# Returns "Item" for "Inventory::ItemSerializer"

But the actual generated type name joins all namespace segments: InventoryItem. This mismatch caused self-references to slip through the import filter.

Solution

Use the name method which already returns the correctly formatted type name, ensuring self-references are properly excluded from imports.

  The self_type_name method was only capturing the last segment of namespaced
  serializer names (e.g., 'Issuance' from 'CommercialInventory::IssuanceSerializer'),
  but the actual generated type name includes all segments ('CommercialInventoryIssuance').

  This caused self-referential associations to incorrectly import their own type,
  resulting in TypeScript error TS2440.
@chris-mangrove chris-mangrove changed the title Fix/self referencing imports Fix self-referencing imports for namespaced serializers Dec 23, 2025
@skryukov
Copy link
Owner

Hey @chris-mangrove, could you provide a serializer example? I can't repro the issue

@skryukov
Copy link
Owner

Ok, my guess is that you are calling typelize parent: 'InventoryItem', if so, the fix is here: #74

@skryukov skryukov closed this Dec 29, 2025
@chris-mangrove
Copy link
Author

Awesome - thank you @skryukov; #74 did fix my issue.

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.

2 participants