StoicWanderer
StoicWanderer2y ago

Parameter of v.array()

Hello, i have a question about using v.array(), do i need to define the values of the array when i use it in my schema, because TS server underlines it that values sre missing? What if want to provide values later to the array?
4 Replies
RJ
RJ2y ago
You need to define the types of the values that you'll be storing the array. So if you want the array to contain strings, you'd write v.array(v.string()). Or if you want it to contain strings and numbers you would write v.array(v.union(v.string(), v.number())). And so on
StoicWanderer
StoicWandererOP2y ago
I see, i will try this again, because when i typed v.array(v.string()) TS underlined it as incorrect yesterday.
ballingt
ballingt2y ago
A general typescript tip is that sometimes you need to cast an empty array to the type you want since it can't be inferred from the value. I think
const a: number[] = [];
const a: number[] = [];
works but some cases require casting the empty array like [] as number[]
RJ
RJ2y ago
If you run into this again, share the error message with us!

Did you find this page helpful?