arktypea
arktype2y ago
2 replies
francis

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:
function arktypeUnionFromEnum<A extends string, B extends string[]>(pgEnum: {
  enumValues: [A, ...B];
}) {
  return type("===", ...pgEnum.enumValues);
}

This fails on the spread argument with
Argument of type 'A' is not assignable to parameter of type 'conform<A, unknown>'.
  Type 'string' is not assignable to type 'conform<A, unknown>'.ts(2345)


Unwinding the generic produces different errors:
function arktypeUnionFromEnum<T extends string[]>(pgEnum: { enumValues: T }) {
  return type("===", ...pgEnum.enumValues);
}

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.
Was this page helpful?