When converting XML to JSON, my library outputs {MyKey: T}
for single-element lists and {MyKey: T[]}
for multi-element lists. The equivalent TypeScript type is
type XmlJsonArray<T, element extends string> = Record<element, T | T[]>
. To implement this as a Zod schema, I have used the following code:
const XmlJsonArray = <T, element extends string>(element: element, schema: z.Schema<T>) => {
// TODO what is the best practice here?
const outerSchema: Record<element, z.Schema<T | T[]>> = {} as any;
outerSchema[element] = z.union([schema, z.array(schema)]);
return z.object(outerSchema);
};
Would it be possible to achieve this without resorting to using any
?