Generic minimum and maximum
Is it possible to create a type that is generic over a min/max value?
I wanted something like this:
but clearly this is the wrong way of thinking about it because min and max are only supported as constants.
I've read through the docs on scope and generics and I feel like the solution is in there somewhere but I can't quite work it out. My use case is that I have an object type that's a union so it can be structured differently but numbers inside of it all share the same min/max values. Currently I have to redefine the entire object's structure to set a min/max value as it's not always the same min/max values.
2 Replies
Good question. It is theoretically possible to allow aliases to specify range constraints then check if they refer to a literal numeric value, but it does add a fair amount of parsing overhead.
Is it possible at all in this situation to just use an external generic wrapper?
https://arktype.io/docs/generics#external
Oh yep, external generic wrapper works perfectly-I just pass in a type that extends number, thank you!