Open ssalbdivad opened 1 year ago
Commenting to note that something like type(...).not()
would also be really handy for JsonSchema -> ArkType (#729), since JsonSchema has a not
'operator' which is currently non-trivial to implement into AT. As an example:
{"not": {"type": "string"}}
@TizzySaurus It is trivial to implement, you just create a predicate that negates the inner type.
It is just not trivial to fully reduce and compare to other types, but that is not needed to translate JSON schema.
This would allow some or all types to be negated (likely using a new
!
operator). The challenge here would be integrating these checks with the type system so that intersections can still be reduced to the fullest extent possible.If this turns out to not be feasible, we could implement it shallowly using a similar approach to narrows where we'd only check equality to determine assignability, or could potentially restrict the set of types that could be negated to literals.
More investigation required.
A couple additional notes after spending a minute thinking about this:
My intuition is that there'd be a "negated" node that could be attached to each branch node. You'd maintain it opposite the normal node- intersections would be attached as unions. It's kind of mind bending that it could essentially continue to invert but maybe the recursion would work everything out and it would be doable. All the second level negations would be intersected back up to the base type? But what if they're in branches of a union? That seems like a problem.
A couple notes based on a followup conversation: