-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
union of intersections doesn't work #897
Comments
And by the way: in |
Actually, I have found the reason of my problems:
This is impossible because
Th only real issue I see here is that error message that I saw is vague - doesn't help to understand what's wrong. |
@vsapronov I've been running into the same problems as you so I just released It adds a new struct type based on the JSON typedef discriminator type. Usage in your example: import * as t from './superstruct'
import { discriminator } from '@birchill/discriminator';
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 = discriminator('_type', {
created: TOrderCreated,
changed: TOrderChanged,
canceled: TOrderCanceled,
})
type OrderEvent = t.Infer<typeof TOrderEvent>
// Produces
//
// type OrderEvent = {
// _type: 'created';
// id: string;
// sku: string;
// quantity: number;
// } | {
// _type: 'changed';
// id: string;
// quantity: number;
// } | {
// _type: 'canceled';
// id: string;
// } When it fails to validate, it will tell you the specific field that failed, e.g.
|
@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. |
Here're types definitions:
Clearly I'm using
_type
as discriminator field, but I don't want to mix it with the "payload" - specific events. Hence I'm doingintersection
for adding discriminator field.I would expect this to work properly:
However I get error:
The text was updated successfully, but these errors were encountered: