How do you handle incompatible null/undefined from discord.js typings?

Note: This is not a typescript problem, I know how to solve it from different ways. I'm just looking for feedbacks or djs-specific answers/workarounds There are a lot of places where discord.js uses a mix of null/undefined and sometimes it gets really frustrating. For example this is a very common occurence in my codebase :
new EmbedBuilder()
.setAuthor({
name: myText,
iconURL: member.user.avatarURL(),
// ^^^^^^^ ^^^^^^^^^^^
// string | undefined string | null
})
new EmbedBuilder()
.setAuthor({
name: myText,
iconURL: member.user.avatarURL(),
// ^^^^^^^ ^^^^^^^^^^^
// string | undefined string | null
})
This leads to a type mismatch which triggers typescript. Is it expected that types within a single library don't match like this? How do you handle this in your discord.js codebase? I'm tired of adding ?? undefined (which makes no sense) or a TS null assertion !… Thank you! (PS: you should update the djs versions in the tags)
7 Replies
Favna
Favna14mo ago
.avatarURL() ?? undefined it's the only way
Elliot
Elliot14mo ago
:/ Do you think it's worth oppening a GH issue? At least to have optional object properties accept nulls (iconURL?: string | null, etc)
Favna
Favna14mo ago
I highly doubt DJS would accept a PR like that tbh but maybe @vladdy will contradict me
vladdy
vladdy14mo ago
Yes Probably would, can't guarantee Sounds like discord issues, just try again Wish i could help but idk why that's happening Update your app See if that solves it
Elliot
Elliot14mo ago
i'll make the issue and let you debate over there
Elliot
Elliot14mo ago
GitHub
Homogenise null and undefined typings within djs APIs · Issue #9829...
Which application or package is this feature request for? discord.js Feature Often when using discord.js APIs together, types are incompatible because of undefined/null discrepancies. This is very ...
Elliot
Elliot14mo ago
probably should have made a discussion now that i think of it oh well
Want results from more Discord servers?
Add your server