djbalinD
Convex Communityโ€ข13mo agoโ€ข
9 replies
djbalin

Narrowing function return type

Hi, consider the toy example below: passing
true
as an argument causes the function to return a
number
while passing
false
causes it to return a boolean. The inferred return type is however always number | boolean regardless of what argument is passed.

I guess this is just a quirk with TS not narrowing function signature based on arguments passed. Is it possible to achieve this however in an elegant way? ๐Ÿ™‚

export const returnNumOrBool = authQuery({
  args: {
    returnNumber: v.boolean(),
  },
  handler: async (ctx, { returnNumber }) => {
    if (returnNumber === true) {
      return 1
    } else {
      return false
    }
  },
})
Was this page helpful?