question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

union of intersections doesn't work

See original GitHub issue

Here’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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
birtlescommented, Sep 9, 2021

@birtles that’s awesome! If you were interested in PR’ing I’d be open to it.

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.

0reactions
ianstormtaylorcommented, Sep 8, 2021

@birtles that’s awesome! If you were interested in PR’ing I’d be open to it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found