Skip to content

Conversation

@Fizzixnerd
Copy link
Contributor

Variants (alternatively known as "sum types", "discriminated unions", or "tagged unions") are a way to define a type that can be one of several different types, but only one at a time. This is useful for representing data that can take on multiple forms, such as for JSON data that can be one of several different types (e.g. a string, number, object, array, etc.).

This implementation includes an exported ExactlyOne type as well, that represents a single branch of a variant.

@Fizzixnerd Fizzixnerd requested review from a team as code owners August 21, 2025 16:49
`Variant`s (alternatively known as "sum types",
"discriminated unions", or "tagged unions") are a
way to define a type that can be one of several
different types, but only one at a time.
This is useful for representing data that can
take on multiple forms, such as for JSON data
that can be one of several different types
(e.g. a string, number, object, array, etc.).

static toFields(value: ExactlyOne<Ts>): Field[] {
const key = getKey(value) as keyof As;
const typedValue = value as any;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this is an internal hack to get this to compile without errors. It is true that if x is keyof ExactlyOne<Ts> then x is keyof As, but I couldn't prove it to typescript easily, so I cast to any here.

@Fizzixnerd Fizzixnerd self-assigned this Aug 22, 2025
@Fizzixnerd Fizzixnerd marked this pull request as draft August 25, 2025 18:29
@bleepbloopsify bleepbloopsify removed the request for review from Geometer1729 October 23, 2025 15:26
@Trivo25 Trivo25 closed this Nov 19, 2025
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.

3 participants