Tom
Tom
Aarktype
Created by Tom on 1/20/2025 in #questions
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!
2 replies
Aarktype
Created by Tom on 1/7/2025 in #questions
Can arktype drop data not covered by schema?
I want to regurgitate my input according to the schema model, e.g:
const user = type({
name: "string",
})

const out = user({name: "bob", age: 51})
const user = type({
name: "string",
})

const out = user({name: "bob", age: 51})
I'd want to make out contain only {name: "bob"}. Is there a way to do this with arktype?
8 replies