union of intersections doesn't work
See original GitHub issueHere’re types definitions:
import * as t from './superstruct'
const TOrderCreated = t.object({
id: t.string(),
sku: t.string(),
quantity: t.number(),
})
export const TOrderChanged = t.object({
id: t.string(),
quantity: t.number(),
})
const TOrderCanceled = t.object({
id: t.string(),
})
const TOrderEvent = t.union([
t.intersection([t.type({_type: t.literal('created')}), TOrderCreated]),
t.intersection([t.type({_type: t.literal('changed')}), TOrderChanged]),
t.intersection([t.type({_type: t.literal('canceled')}), TOrderCanceled]),
])
type OrderEvent = t.Infer<typeof TOrderEvent>
Clearly I’m using _type
as discriminator field, but I don’t want to mix it with the “payload” - specific events. Hence I’m doing intersection
for adding discriminator field.
I would expect this to work properly:
let event: OrderEvent = t.create({ _type: 'changed', id: 'id123', quantity: 123 }, TOrderEvent)
However I get error:
StructError: Expected the value to satisfy a union of `intersection | intersection | intersection`, but received: [object Object]
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Counterexample, union of intersections, intersection of unions
I am looking for an example which illustrates that the following doesn't have to ...
Read more >How to resolve Intersection and Union doesn't match in ts?
For more on this issue, see this SO answer. You can nevertheless achieve what you want via an explicit return type (return types...
Read more >Infinite Unions and Intersections
It's often necessary to work with infinite collections of sets, and to do this, you need a way of naming them and keeping...
Read more >Union and intersection contracts are hard, actually - Tweag
The existing literature, while keenly aware of the fact, doesn't really explain why. This post intends to explain and illustrate this why. You' ......
Read more >4.3: Unions and Intersections - Math LibreTexts
Given two sets A and B, define their intersection to be the set ... as Democrats and were union members, or did not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks! Sure, it will take a bit of work to get it fully ready but I’ll try to get to it in the coming weeks.
@birtles that’s awesome! If you were interested in PR’ing I’d be open to it.