arktypea
arktypeโ€ข16mo ago
aabad_ankit

Can I add a default value to a type if everything other satisfies in a union type

const type1 = type({name: "string"})
const type2 = type({city: "string"})
const union = type1.or(type2)
const result1 = union({name:"ankit"})
const result2 = union({city:"Ajmer"})
I want result1 to be {name: "ankit",kind:"name"}
I want result2 to be {city:"Ajmer",kind:"city"}
or better
const [kind,result] = union({name:"ankit"})
here kind will be "name" and result will be {name:"ankit"} and kind type will be "name"| "city"
can I add these default value of kind in the type definitions so that if parsed with that validator from the union the kind is added
Was this page helpful?