Josh
Josh
Aarktype
Created by Josh on 4/22/2025 in #questions
Generic minimum and maximum
Is it possible to create a type that is generic over a min/max value? I wanted something like this:
type('<min extends number, max extends number>', 'min <= number <= max')
type('<min extends number, max extends number>', 'min <= number <= max')
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.
3 replies
Aarktype
Created by Josh on 3/24/2025 in #questions
Trying to index an array type with .get(...path)
const schema = type('string[]');
console.log(schema.get(0).expression);
const schema = type('string[]');
console.log(schema.get(0).expression);
The code below prints "string | undefined" which at first kind of makes sense but after thinking about it, I feel like it should just be "string". While regular array indexes can return undefined, types are never actually turned into real values so an index can never actually fail. It also doesn't match TypeScript types where indexing an array type like string[][0] equals string. My use case is that I'm building an automatic form generator where I am using .get(...path) to get a specific part of the schema in a deeply nested object schema. I want to be able to effectively get the array's type but to my knowledge it's impossible to do so because any array index always adds | undefined so I cannot differentiate between string[] and (string | undefined)[] for example.
37 replies