get-convex / convex-helpers

A collection of useful code to complement the official packages.
MIT License
66 stars 8 forks source link

Using partial with top level optional field removes possible type #119

Closed Entropy-10 closed 1 month ago

Entropy-10 commented 2 months ago

When using the partial function with an object that has non optional and optional top level fields, any field marked as optional will only return the undefined type.

example table

export const Users = Table('users', {
  clerkId: string,
  stripeId: optional(string),
  username: string,
  pastUsernames: optional(
    array(object({ username: string, createdAt: number }))
  ),
  tag: string,
  pastTags: optional(array(object({ tag: string, createdAt: number }))),
  personalInfo: optional(
    object({
      age: optional(number),
      pronouns: optional(string),
      preferredName: optional(string)
    })
  )
})

patch mutation

export const updateUser = userRLSMutation({
  args:  {
    id: v.id('users'),
    patch: object(partial(Users.withoutSystemFields))
  },
  handler: async (ctx, args) => {
    await ctx.db.patch(args.id, args.patch)
  }
})

mutation on client This returns a type error as it says personalInfo can only be set to undefined.

const updateUserMutation = useMutation(api.user.updateUser)
await updateUserMutation({
  id: user._id,
  patch: { personalInfo: { ...data } }
})

type generated by patch mutation

patch: {
  clerkId?: string | undefined;
  stripeId?: undefined;
  username?: string | undefined;
  pastUsernames?: undefined;
  tag?: string | undefined;
  pastTags?: undefined;
  avatarId?: undefined;
  personalInfo?: undefined;
 }
}
ianmacartney commented 2 months ago

Thanks for the report! I think I just fixed it in version convex-helpers@0.1.36-alpha.0 Can you try it out and confirm it behaves as you expect at runtime & with types? I cut an alpha b/c I didn't have time to verify myself

Entropy-10 commented 2 months ago

Yep that fixed the issue perfectly!

ianmacartney commented 2 months ago

Great, I will get that into the next patch release 🎉

On Thu, May 2, 2024 at 7:45 AM Entropy @.***> wrote:

Yep that fixed the issue perfectly!

— Reply to this email directly, view it on GitHub https://github.com/get-convex/convex-helpers/issues/119#issuecomment-2090691090, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACZQW3UIMF3AT2RTNFNJ2DZAJGRJAVCNFSM6AAAAABHC5YCYGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJQGY4TCMBZGA . You are receiving this because you commented.Message ID: @.***>