beerman
beerman3w ago

Type Mismatch in Update Mutation with Optional Fields

I’m running into a type mismatch issue when updating an account using a mutation. Simplified version of my setup: schema.ts
export const account = v.object({
name: v.string(),
type: accountType,
balance: v.number(),
totalDeposited: v.number(),
totalWithdrawn: v.number(),
profitLoss: v.number(),
commission: v.number(),
notes: v.string(),
isActive: v.boolean()
});
export const account = v.object({
name: v.string(),
type: accountType,
balance: v.number(),
totalDeposited: v.number(),
totalWithdrawn: v.number(),
profitLoss: v.number(),
commission: v.number(),
notes: v.string(),
isActive: v.boolean()
});
accounts.ts
onst { type, ...updates } = account.fields;

export const update = mutation({
args: v.object({
accountId: v.id('accounts'),
...updates
}),
handler: async (ctx, args) => {
const { accountId, ...updates } = args;
await ctx.db.patch(accountId, updates);
}
});
onst { type, ...updates } = account.fields;

export const update = mutation({
args: v.object({
accountId: v.id('accounts'),
...updates
}),
handler: async (ctx, args) => {
const { accountId, ...updates } = args;
await ctx.db.patch(accountId, updates);
}
});
component
async function handleUpdateAccount(
accountId: Id<'accounts'>,
updates: Partial<Omit<WithoutSystemFields<Doc<'accounts'>>, 'type'>>
) {
await client.mutation(api.accounts.update, {
accountId,
...updates
});
}
async function handleUpdateAccount(
accountId: Id<'accounts'>,
updates: Partial<Omit<WithoutSystemFields<Doc<'accounts'>>, 'type'>>
) {
await client.mutation(api.accounts.update, {
accountId,
...updates
});
}
Error
Types of property 'name' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
• In the mutation, I’ve excluded the type field and allowed the remaining fields to be updated. • The handleUpdateAccount function uses Partial so only the fields that need updating are passed. • I suspect the issue is that the mutation expects all fields to be required, but I’m passing optionals. Is there a proper way to define the mutation to accept optional fields without manually marking each field as optional? Should I adjust the schema or is there a better solution to handle partial updates?
2 Replies
Convex Bot
Convex Bot3w 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!
beerman
beermanOP3w ago
I resolved the issue using validator utilities like partial, pick, and omit to match the TypeScript type utilities. https://github.com/get-convex/convex-helpers/blob/main/packages/convex-helpers/README.md#validator-utilities
GitHub
convex-helpers/packages/convex-helpers/README.md at main · get-conv...
A collection of useful code to complement the official packages. - get-convex/convex-helpers

Did you find this page helpful?