Skip to content

Missing overrides for .toString() in partial types #9833

@janparisek

Description

@janparisek

Which package is this bug report for?

discord.js

Issue description

I've discovered incorrect typings in some of the partial types. Particularly, the .toString() method supposedly always evaluates to "[object Object]", despite this never being the case during runtime.

Why I believe I'm correct:

  • <GuildMember>.toString() returns a UserMention which is (ultimately) based on <GuildMember>.user.id
  • <GuildMember>.id returns <GuildMember>.user.id
  • The id property is guaranteed to exist on all partial types (see Partialize type)
  • Therefore, <PartialGuildMember>.user.id is also guaranteed to exist
  • That is to say: PartialGuildMembers are always mentionable
  • Consequently, <PartialGuildMember>.toString() should always yield a string like the non-partial variant does

During runtime, PartialGuildMember always has a .toString() method attached.
However, typescript-eslint (extending plugin:@typescript-eslint/strict-type-checked) tells me that the latter may evaluate to "[object Object]" (@typescript-eslint/no-base-to-string).

This problem also exists on other partial types. I have yet to look a bit further into this, but so far I've identified this to potentially be the case with User, Channel, MessageReaction and ThreadMember.

Code sample

/* eslint-disable @typescript-eslint/no-unused-vars */

import { GuildMember, PartialGuildMember } from "discord.js";

// No linting error here
function test1(member: GuildMember) {
  console.log(`${member.toString()}`);
}

// Throws @typescript-eslint/no-base-to-string with typescript-eslint
function test2(member: PartialGuildMember) {
  console.log(`${member.toString()}`);
}

Versions

  • discord.js 14.13.0

  • Node.js 18.17.1

  • TypeScript 5.2.2

  • Windows 10 Pro N 22H2 Build 19045.3086 with Experience 1000.19041.1000.0

  • eslint 8.48.0

  • @types/node 18.17.1

  • @typescript-eslint/eslint-plugin 6.6.0

  • @typescript-eslint/parser 6.6.0

Issue priority

Low (slightly annoying)

Which partials do you have configured?

GuildMember

Which gateway intents are you subscribing to?

GuildMembers

I have tested this issue on a development release

0d40855.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions