union of two types
how do i typeguard in convex?
in typescript i usually do type guards to handle this situtation, how to do that in convex?
function isText(invoice: args ): invoice is object1 {
return invoice.type === "text";
}
10 Replies
args
is an object with properties, it should be typed based on the args validator so this should be normal TypeScript syntax. What's value
here?I needed up with more simple solution:
ā Error fetching POST https://proper-gnu-660.convex.cloud/api/push_config 400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Unable to parse JSON from
exportArgs
: Args validator must be an object or any
ok this is not working...What are text and media?
The top-level args value needs to be an object
they are objects in other files...
export const text = v.object({...});
@jamalsoueidan and media?
same in another file
Ah you're right, the top level can't be a union!
you'll have make your union validator a property of the top-level arguments object, e.g.
```ts
const args = v.union(text, media);
type Args = Infer<typeof args>;
type Text = Infer<typeof text>;
function isTextMessage(args: Args): args is Text {
return args.entry[0].changes[0].value.messages[0].type === "text";
}
export const webhooks = internalAction({
{ args },
handler: async (ctx, { args }) => {
if (isTextMessage(args)) {
await ctx.runAction(internal.datatypes.text.run, args);
Sounds like a bug that this isn't a TypeScript error, you have to wait until runtime
okay first day first bug...
my room blinking red,,,
i get flashback from the past when i used bit.cloud
damn it was the worst experience in my life...i was fighting against the system full of bugs hahah...but im sure convex is not like that
ooof, we absolutely aim for it not to be like that! If you report something here and it's part of the core of Convex like this we will get it fixed.
thank you...i can already feel its good community here š