I have the following code
export const taxTypeArray = [
{value: 'INCLUSIVE' as const, label: '外税'},
{value: 'EXCLUSIVE' as const, label: '内税'},
]
const test = taxTypeArray.map((item) => item.value)
export const taxType = z.enum(test)
and it gives the following error:
Argument of type '("INCLUSIVE" | "EXCLUSIVE")[]' is not assignable to parameter of type 'readonly [string, ...string[]]'.
the reason is that the type of test needs to be
type ["INCLUSIVE", "EXCLUSIVE"]
but array.map outputs the following type
type ("INCLUSIVE" | "EXCLUSIVE")[]
Is there a way I can change it so that test
is of a type that z.enum
can take?
I'd like to keep the types intact so I'd rather not use the following:
const test = taxTypeArray.map((item) => item.value) as [string, ...string[]]
I would not use
.map
to calculate the array, this contains JS logic. Usez.nativeEnum()
and declare theTaxType
TS enum. Keep code simple and with as little logic as possible.