Sara
Sara2w ago

can I do this type defenition like in typescript with v validator?

I tried doing:
const textData = literals('x','y','z');
const typeOfTextDataInRecord = v.record(textData, v.array(v.string()))
const textData = literals('x','y','z');
const typeOfTextDataInRecord = v.record(textData, v.array(v.string()))
same as in typescript in my brain as doing this:
type textData = ['x','y','z'];
type howWeDoTextData = textData[number]
type typeOfTextDataInRecord = Record <howWeDoTextData,string[]>
type textData = ['x','y','z'];
type howWeDoTextData = textData[number]
type typeOfTextDataInRecord = Record <howWeDoTextData,string[]>
3 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Sara
SaraOP2w ago
update: I created an object that has keys and values and the type seems to resemble what I want.
const newType = v.object({key:textData, value:v.array(v.string())})
const newType = v.object({key:textData, value:v.array(v.string())})
sshader
sshader2w ago
Yeah the best way of getting the validation you want is storing an array of objects with key and value types (and then unpacking them into a record or map in your code if you prefer). Fun fact -- TypeScript's Record type has some kind of funky behavior when the key is a union of literals example This is one of the reasons why we don't allow things like v.record(textData, <other validator>) and just allow things like v.record(v.string(), <other validator>)
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.

Did you find this page helpful?