-
Notifications
You must be signed in to change notification settings - Fork 167
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
Discussion: Can oneOf
be implemented?
#151
Comments
@rubenferreira97 Hi, thanks for the suggestion. I'm not sure if this is something that could be implemented in TypeBox (at least not as a core primitive type). The preference for TypeBox is to only implement basic primitives that can be composed together to form higher kinded types. So the preference would be to provide a sufficient set of core primitives in TypeBox to allow the composition of a Looking at your example tho type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends Object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
type AllXOR<T extends any[]> =
T extends [infer Only] ? Only :
T extends [infer A, infer B, ...infer Rest] ? AllXOR<[XOR<A, B>, ...Rest]> :
never; So to implement this in a way that aligns with the goals of TypeBox, TypeBox would need to implement both conditional types as well as schema inference for Conditional type mapping is something I would like to explore in TypeBox one day, however implementing an const T = Type.Extends(LeftType, RightType, TrueType, FalseType)
// T = Type.Literal(true)
const T = Type.Extends(Type.String(), Type.String(), Type.Literal(true), Type.Literal(false))
// T = Type.Literal(false)
const T = Type.Extends(Type.String(), Type.Number(), Type.Literal(true), Type.Literal(false)) Unfortunately, the logic to evaluate the Happy to discuss things more, particularly if you have a draft implementation on how the above |
@rubenferreira97 Hi, just a follow up on this issue. I have explored implementing conditional mapping (including support for I may investigate this functionality again in future, but for now will be putting on the back burner (until such time as the TypeScript type system has settled down a bit more) Will close off this issue for now. |
Currently is not possible to write a schema, where the given data must be valid against exactly one of the given subschemas.
I am almost certain that this was not implemented since it's hard to express on Typescript.
However, after some search I think it's possible to achieve a
type
"XOR" by addingnever
to the remaining properties (Don't know if it solves every problem though).Playground from microsoft/TypeScript#14094:
The text was updated successfully, but these errors were encountered: