feat: add discriminated union types to nitrogen - #1590
Open
dhruvpatel374 wants to merge 3 commits into
Open
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
dhruvpatel374
force-pushed
the
feat/discriminated-union-types
branch
from
September 5, 2026 06:47
a398c65 to
1003582
Compare
dhruvpatel374
force-pushed
the
feat/discriminated-union-types
branch
from
September 5, 2026 14:50
1efc067 to
16d5cbe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1466
What
Adds support for discriminated union types in nitrogen. When a TypeScript union consists of interfaces that share a common string literal property, nitrogen now detects the discriminant key and generates a named
JSIConverterthat dispatches on it at runtime instead of falling back to positionalstd::variantindexing.Here
kindis the discriminant. At runtime, ifkindis"truck"the converter deserializes aTruck, if"boat"it deserializes aBoat. This also solves the ambiguous-all-nulls problem from the issue - even when every other property is optional/null, the discriminant tells the converter exactly which struct to construct.How it works
createType.ts- addedfindDiscriminantKey()that inspects a union's constituents for a shared property where every member has a unique string literal value. If found, returns aDiscriminatedUnionTypeinstead of a plainVariantType.getInterfaceProperties.ts- added optionalskipKeysparam so the discriminant property is excluded from the struct fields (the union'sJSIConverterhandles dispatch, the struct itself doesn't need it).CppDiscriminatedUnion.ts- generates aJSIConverter<std::variant<A, B>>specialization that reads the discriminant key first, then delegates to each struct's own converter.SwiftDiscriminatedUnion.ts- generates a@frozen enumwith cases named after discriminant values (e.g..truck,.boat) instead of positional.first,.second.KotlinDiscriminatedUnion.ts- generates asealed classwith inner classes named after discriminant values.SwiftCxxBridgedType,KotlinCxxBridgedType,SwiftCxxTypeHelper, andgetReferencedTypes.Generated output
C++ (
Vehicle.hpp):Swift (
Vehicle.swift):Kotlin (
Vehicle.kt):Test
Truck,Boat,Vehicle(discriminant keykind) toTestObject.nitro.tsbounceVehiclemethod to C++, Swift, and Kotlin test implementationsgetTests.tscovering both thetruckandboatcasesChecklist
bun specsrun and generated files committedbun lint-allpassesbun typecheckpasses