How to accept a generic non-empty const string array for use in `type`?
Note: this may be a TS question, feel free to send me there instead.
I have the following type:
PgEnum<TValues extends [string, ...string[]]> { enumValues: TValues; }
I'm attempting to create a function which accepts this object and returns an arktype instance of the union of the strings in the array, which is const with well-known values.
If I call it directly from the const signature, e.g. type('===', obj.enumValues)
, it works fine. But I can't figure out how to accept this as a generic function parameter and use it in the body.
I have tried:
This fails on the spread argument with
Unwinding the generic produces different errors:
leads to A spread argument must either have a tuple type or be passed to a rest parameter.
Replacing string[]
with [string, ...string[]]
leads to the same issue.
I suspect the two generic parameters is the closer approach, but I have no idea what this conform<>
error is, or how to begin to address it.2 Replies
This just looks like a problem where TS gets confused about inferring types associated with generic parameters from within a function.
There may be a way to do it, but to be honest I'd probably just annotate the return type as e.g.
Type<A | B[number]>
and cast the args you pass internally so TS doesn't complain about them.
If you look at ArkType's implementation you'll find casting is often inevitable in situations like this, even when the generics you're wrapping are relatively straightforward.
The good thing is, you're only losing safety on one line. As long as you're confident the annotated return type is correct, you'll still have a safe result every time you invoke the function.thank you! I really appreciate your expertise!