Incorrect typings for `.toString()` in partials?
Alright, I'm not sure whether or not I've found an issue, so correct me if I'm wrong.
-
<GuildMember>.toString()
is (ultimately) based on <GuildMember>.user.id
- <PartialGuildMember>.user.id
exists, since it's the only property to be guaranteed on partials
- Consequently, <PartialGuildMember>.toString()
should yield a string similar to the non-partial variant
- However, ESLint tells me that the latter may evaluate to "[object Object]"
(@typescript-eslint/no-base-to-string)
Is there something I'm getting wrong? Is this intended behavior?24 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- β
Marked as resolved by OPUnknown Userβ’16mo ago
Message Not Public
Sign In & Join Server To View
Give me 15 minutes
Unknown Userβ’16mo ago
Message Not Public
Sign In & Join Server To View
PartialGuildMember is a djs type
Unknown Userβ’16mo ago
Message Not Public
Sign In & Join Server To View
Yes
It's not in the docs because it's just a type
Yeah, that. TypeScript forces me to use it (for good reasons I suppose, unless I got something fundamentally wrong).
Do you use the
GuildMember
partial?
(Check your client declaration)-
discord.js 14.13.0
& node 18.17.1
- It's a discord.js issue
- No error stack trace, this is about typings
- Code: Repo Line 22. Added Line 45 to illustrate the issue.
Thank you for reopening!
I thought clicking the "solved" button would mean I solved the questionnaire I got from the bot π€£
Yup, I do use the GuildMember partial on client creation.Idk why toString() is a different type, but I believe it would be fine for you to just use the
userMention()
function
userMention(member.id)
I mean, that'd be a workaround, but it still wouldn't change the fact that this causes TypeScript to throw an error for no good reason.
Looking at the d.js code, I don't see how
<PartialGuildMember>.toString()
could ever evaluate to "[object Object]"
in practice.
I believe the interface PartialGuildMember extends Partialize<>
part in line 6131 of the typings are at fault here.
Ideally, toString()
should not be axed during the "partialization".
However, I'm not 100% certain if this is intended behavior (and I'm missing something). Otherwise I'd have started a PR or maybe even attempted a fix myself already.Yeah, I checked that, don't know why it would mess up toString's typings
Might need to look at it closer when I'm on my pc
Alright. Thanks for taking your time, by the way.
What's your TypeScript version? I am trying to reproduce it with this code and no errors for me:
I could reproduce the ESLint error with this setup:
package.json
tsconfig.json
.eslintrc.cjs
file.ts
However, no TypeScript errors.
Ah, I see it now
It looks like an ESLint issue.
I kept the same setup but replaced
file.ts
with this : https://pastebin.com/y6RfmkHQ.
I started the bot and gave myself a role on a test server the bot is in. It received a partial guild member and I called .toString() on it which logs the expected user mention.Actually my IDE says the
.toString()
is coming from Object, so there might be somehting wrong with the types but it works somehowYeah, TypeScript seems to be fine. I think it assumes Node will implicitly add
.toString()
to all objects.
It's only getting caught by ESLint (or more specifically the plugin typescript-eslint
) with the strict-type-checked
configuration (plugin:@typescript-eslint/strict-type-checked
).
5.2.2A djs contributor told me the problem is with
Partialize
. Apparently the & is in the wrong order (hopefully I understood that correctly Oh, Actually, maybe someone already opened an issue, let me check.
Partialize
sounds familiar.
I was looking into the typings yesterday, seeing if I could fix the issue and it absolutely baffled me.
That is, because it's WAY above my TS expertise and I couldn't understand a single thing.
And it certainly didn't help that the 3 generic type names N
, M
and E
were non-descriptive.
Is someone already looking into this, or should I open an issue on GH?
Same problem here, waiting for a fix
Iβm already working on a fix. Itβs almost done, but Iβm still wrestling the build toolchain as my tests keep failing.