ballingt
ballingt3y ago

Spread Parameter Question

const c = [s.string(), s.boolean()] as const;
s.union(...c);
const c = [s.string(), s.boolean()] as const;
s.union(...c);
should do it!
2 Replies
Airplane
Airplane3y ago
yep that worked thanks! My use case is a little more complex though so it's still giving me an error - any way to make something like this work?
const c = [s.string(), s.boolean()] as const;

s.union(...c.map((a) => s.object({ keyName: a })));
const c = [s.string(), s.boolean()] as const;

s.union(...c.map((a) => s.object({ keyName: a })));
ballingt
ballingtOP3y ago
this starts to get complicated, I'd do this the long way with
const c = [
s.object({ keyName: s.string()}),
s.object({ keyName: s.boolean()})
] as const;
const c = [
s.object({ keyName: s.string()}),
s.object({ keyName: s.boolean()})
] as const;
because otherwise you have to start writing fancy types — but this suggests a utility to replace map that could make this work. Before we go deeper, what are you trying to do, big picture? I ask because if we go this route we're going to be writing code that should probably be part of the schema builder if this is a generally useful thing.

Did you find this page helpful?