Gorka Cesium
Gorka Cesium3mo ago

How to assign types created with convex/values to function parameters?

I want to create a utility function but need to pass an argument that has type safety. How can I reuse the type from a convex/values into a normal function? Example schema.ts
export const cut = v.union(v.literal("Rect"), v.literal("Curve");


export default defineSchema({
things: defineTable({
cut: cut
})
})
export const cut = v.union(v.literal("Rect"), v.literal("Curve");


export default defineSchema({
things: defineTable({
cut: cut
})
})
Here I have my utility function. I can use some hacks around it but the compiler won't know when the cut definition changed. utils.ts
export let cutToText = (cut: any) => {
return cut === "Rect" ? "Rectangle": cut === "Curve" ? "Curvy" : ""
}
export let cutToText = (cut: any) => {
return cut === "Rect" ? "Rectangle": cut === "Curve" ? "Curvy" : ""
}
6 Replies
Convex Bot
Convex Bot3mo 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!
lee
lee3mo ago
Schemas | Convex Developer Hub
Schema validation keeps your Convex data neat and tidy. It also gives you end-to-end TypeScript type safety!
Gorka Cesium
Gorka CesiumOP3mo ago
ah yeah, that looks like it will help. Thanks!
Gorka Cesium
Gorka CesiumOP3mo ago
thank you works like a charm

Did you find this page helpful?