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
Favna16mo ago
.avatarURL() ?? undefined it's the only way
Elliot
ElliotOP16mo 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
Favna16mo ago
I highly doubt DJS would accept a PR like that tbh but maybe @vladdy will contradict me
vladdy
vladdy16mo 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
ElliotOP16mo ago
i'll make the issue and let you debate over there
Elliot
ElliotOP16mo 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
ElliotOP16mo ago
probably should have made a discussion now that i think of it oh well
Want results from more Discord servers?
Add your server