A
arktype3d ago
Tom

How to ensure optimal discrimination of union types?

I have a union of types, each of which has an attribute with literal values that can in theory be used to quickly identify an appropriate top-level type. They all look like this:
MyModelV1_0: {
// model specific fields
times: "number[]",
...,
// constant field
schema_version: {
major: "1",
minor: "0",
"patch?": "number.integer >= 0 | null",
"+": "delete",
},
"+": "delete",
},
MyModel: "MyModelV1_1 | MyModelV1_0",
MyModelV1_0: {
// model specific fields
times: "number[]",
...,
// constant field
schema_version: {
major: "1",
minor: "0",
"patch?": "number.integer >= 0 | null",
"+": "delete",
},
"+": "delete",
},
MyModel: "MyModelV1_1 | MyModelV1_0",
Is there some way I can setup my types or configure arktype such that it does a breadth-first discrimination of types within a union? does it store the result of the matching type somewhere? and finally - I'd like to understand this logic a bit more, but I am completely unfamiliar with the code base, if someone could point me at the code for resolving type unions that would be very useful. Thanks!
1 Reply
ssalbdivad
ssalbdivad2d ago
Yes ArkType will do this by default. If you want to see what the structure of the discriminated union looks like, you could try something like myModel.internal.assertHasKind("union").discriminantJson

Did you find this page helpful?