Skip to content

Conversation

@puckey
Copy link
Contributor

@puckey puckey commented Dec 1, 2025

When TypeScript flattens boolean | SomeEnum into boolean | 'foo' | 'bar', the named enum type information is lost. This PR reconstructs the original enum types by searching all type aliases in the project and matching their string literal values.

Changes

  • Add initializeTypeCreation() to build a cache of type aliases for efficient lookup
  • Add reconstructEnumTypesFromStringLiterals() to find matching enum types from flattened unions
  • Handle variants with multiple string enums
  • Detect and throw error for overlapping enums
  • Added test cases in TestObject.nitro.ts
  • Added documentation to variants.md

…ariants

TypeScript flattens `boolean | SomeEnum` into `boolean | 'foo' | 'bar'`, losing
the enum type information. This fix reconstructs the original enum types by
searching all type aliases in the project and matching their string literal
values against the flattened union.

- Add `initializeTypeCreation()` to build a cache of type aliases for lookup
- Add `reconstructEnumTypesFromStringLiterals()` to find matching enum types
- Handle variants with multiple string enums (e.g., `SomeEnum | SomeOtherEnum`)
- Detect and throw error for overlapping enums (ambiguous discrimination)
- Add test cases for various enum+primitive combinations
@vercel
Copy link

vercel bot commented Dec 1, 2025

Someone is attempting to deploy a commit to the Margelo Team on Vercel.

A member of the Team first needs to authorize it.

@mrousavy
Copy link
Owner

Hey - thank you so much for this PR, this is pretty cool.

I would say this is a very convenient feature, but I am a bit hesitant on merging it due to the amount of code it requires, and stuff like the "hacky" initializeTypeCreation global function.. I'll think about it :)

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