MietzeKatze
MietzeKatze3d ago

Is there a way to have ts types and v.whatever() types synced?

hey, its getting rather tedious to update my types on so many places. Does anyone know of a more elegant approach? 1. how can I keep both typescript and schema updated at the same time? eg
type todo{
name: string,
newlyAddedThing: string
}
//wish it would also change in schema
defineTable({
todo:{
name: v.string(),
//here should automticaly be newlyAddedThing
})
type todo{
name: string,
newlyAddedThing: string
}
//wish it would also change in schema
defineTable({
todo:{
name: v.string(),
//here should automticaly be newlyAddedThing
})
} 2. if i have 3 different mutations that take a todo as args... how do I prevent having to change the args todo definition on all 3 manualy?
2 Replies
Convex Bot
Convex Bot3d 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!
MietzeKatze
MietzeKatzeOP2d ago
SOLUTION: make new file validators.ts in convex folder and define your validators there, then infer the type
export type todo_type = Infer<typeof todo_validator>;

export const todo_validator = v.object({
name: v.string(),
newlyAddedThing: v.string()
})
export type todo_type = Infer<typeof todo_validator>;

export const todo_validator = v.object({
name: v.string(),
newlyAddedThing: v.string()
})
the type can also be used in frontend magicaly 👍 the validator can be used while defining schemas

Did you find this page helpful?