Do Partials Create Un-Typesafe Code?
If I am listening for say a message update event with partials of messages and I receive a partial message in that event with only the ID and no other information, is it possible for me to access a value that doesn’t exist on the object sent to the event listener?
My assumption would be there’s a Message type and a PartialMessage type and the onMessageUpdate() event listener would give a PartialMessage | Message and you’d use the .partial property to determine the state of it
From the docs though it only sends a message object which could be partially constructed, resulting in accessing a property that’s null - creating a runtime error that could have been caught with types?
3 Replies
Doesn’t matter too much but relevant code if it helps https://github.com/AnswerOverflow/AnswerOverflow/blob/Discord-Event-Parity/apps/discord-bot/src/listeners/parity/message-parity.ts
Short answer: yes.
But sometimes partials are a must have and so we need to work around it in runtime.
Yeah that makes sense, I think the part that surprises me about it is it seems like it could be solved by if partials are enabled then making listener events take in two types, unless it’s more complicated than that
I suppose I could make my own type and use an OR in the function parameter type, I’ll give that a try
For Answer Overflow - it’s important to get those full objects on events so I’ll be fetching them if it’s a partial
Looking at https://discordjs.guide/popular-topics/partials.html#enabling-partials - just to confirm if I go to get something from the cache there’s a chance it’s going to be a partial object so I’ll pretty much always need to check if partial before accessing certain data?
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.