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
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 onI see, i will try this again, because when i typed
v.array(v.string())
TS underlined it as incorrect yesterday.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
works but some cases require casting the empty array like
[] as number[]
If you run into this again, share the error message with us!