CabalDAO
CabalDAO3mo ago

Mapping defined types to other defined types

I have two types:
export const ReferralOverviewStatus = v.union(
v.literal("new"),
v.literal("in_review"),
v.literal("needs_review"),
v.literal("accepted_awaiting_placement"),
v.literal("accepted_won"),
v.literal("accepted_lost"),
v.literal("bed_hold"),
v.literal("rejected")
);
export const ReferralOverviewStatus = v.union(
v.literal("new"),
v.literal("in_review"),
v.literal("needs_review"),
v.literal("accepted_awaiting_placement"),
v.literal("accepted_won"),
v.literal("accepted_lost"),
v.literal("bed_hold"),
v.literal("rejected")
);
And
export const DecisionStatus = v.union(
v.literal("accepted"),
v.literal("rejected"),
v.literal("needs_review")
);
export const DecisionStatus = v.union(
v.literal("accepted"),
v.literal("rejected"),
v.literal("needs_review")
);
As you can see, DecisionStatus overlaps with ReferralOverviewStatus. In one mutation, I accept an args type of DecisionStatus and I want to pass it to a mutation that accepts an arg type of ReferralOverviewStatus. I've tried a bunch of different things but I still get typescript errors for all of these. How can I make this work?
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!
CabalDAO
CabalDAOOP3mo ago
I made a manual type of just plain strings for ReferralOverviewStatus as :
export type ReferralOverviewStatusType =
| "new"
| "in_review"
| "needs_review"
| "accepted_awaiting_placement"
| "accepted_won"
| "accepted_lost"
| "bed_hold"
| "rejected";
export type ReferralOverviewStatusType =
| "new"
| "in_review"
| "needs_review"
| "accepted_awaiting_placement"
| "accepted_won"
| "accepted_lost"
| "bed_hold"
| "rejected";
Then passed it like so:
await ctx.scheduler.runAfter(0, internal.referralOverview.updateStatus, {
referralId: args.referralId,
status: args.decision as ReferralOverviewStatusType,
});
await ctx.scheduler.runAfter(0, internal.referralOverview.updateStatus, {
referralId: args.referralId,
status: args.decision as ReferralOverviewStatusType,
});
This seems to work. But is there any easier way than having to write separate plain js types that are really the same thing as the v type?
M Zeeshan
M Zeeshan3mo ago
@CabalDAO have you tried this approach?
status: args.decision as Doc<YOU_SCHEMA_TABLE_NAME>['filed name']
status: args.decision as Doc<YOU_SCHEMA_TABLE_NAME>['filed name']
e.g.
import { Doc } from './_generated/dataModel';
type SkillType = Doc<"skills">['type'];
import { Doc } from './_generated/dataModel';
type SkillType = Doc<"skills">['type'];
jamalsoueidan
jamalsoueidan3mo ago
The easist way to do that
import { defineSchema, defineTable } from "convex/server";
import { v, Infer } from "convex/values";

export const DecisionStatus = v.union(
v.literal("accepted"),
v.literal("rejected"),
v.literal("needs_review")
);

export type DecisionStatus = Infer<typeof DecisionStatus >;
import { defineSchema, defineTable } from "convex/server";
import { v, Infer } from "convex/values";

export const DecisionStatus = v.union(
v.literal("accepted"),
v.literal("rejected"),
v.literal("needs_review")
);

export type DecisionStatus = Infer<typeof DecisionStatus >;
` Lets gooooo...
CabalDAO
CabalDAOOP3mo ago
thank you for this! very helpful

Did you find this page helpful?