Union of Objects
Table of Contents
Discriminated Unions
What are Discriminated Unions?
Discriminated unions (also called tagged unions) allow you to create a field that can be one of several object types. Each type is identified by a discriminator field (commonly called 'type'), which makes it easy to determine which variant you're working with.
Basic Syntax
Create a discriminated union by specifying the discriminator field name as the first argument:
Using in Content
When you use a discriminated union in your content, Val Studio will show a dropdown to select which variant to use:
Rendering Discriminated Unions
In your React components, you can use TypeScript's type narrowing to handle each variant:
Use Cases
Discriminated unions are perfect for:
Content blocks with different types
Form fields with different input types
Component variants with different props
Any content that can take multiple shapes