Discord.js: [BUG] Broken typing when using typescript

Created on 26 Aug 2020  Â·  4Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:

Both the docs and in-code typing shows that using client.channels.cache.get() returns a Channel object, and that client.channels.cache is of type Collection<string, Channel>. If you are using typescript, doing client.channels.cache.get("some_id").send() will give you the error Property 'send' does not exist on type 'Channel'. This becomes an issue because neither tsc nor ts-node will then run or compile your code.

TL;DR all documentation and code linting complains that client.channels.cache.get("some_id").send() doesn't have a property send. This is wrong, as running the code without linting works just fine.

Include a reproducible code sample here, if possible:

client.channels.cache.get("someTextChannelID").send("Some message");

above: Typescript complains that .send() does not exist on Channel, and won't run your code.

// @ts-ignore
client.channels.cache.get("someTextChannelID").send("Some message");

above: Typescript doesn't complain since it is disabled for that line, and will run the code just fine. It works and runs as intended.

Further details:

  • discord.js version: latest on github, hash 43c4d80b
  • Node.js version: 14.7.0
  • Operating system: Windows 10 64bit, OS-build 19041.329
  • Priority this issue should have – please be realistic and elaborate if possible: High medium/low high

Relevant client options:

  • partials: none
  • gateway intents: none
  • other: none
  • [x] I have also tested the issue on latest master, commit hash: 43c4d80b
question (please use Discord instead)

Most helpful comment

If you think this needs further discussion or treatment please make a separate issue as enhancement suggestion. This is not a bug, but expected behavior.

If you choose to do so please try to use more constructive language and leave out all the venting and insulting.

All 4 comments

This is expected behavior, the caches can hold any channels, channels of type voice or category will of course not have the #send method implemented, after all these channel types can not receive messages as per api specification (and common sense).

You should either type guard or assert (since the ID you provide as key to the #get call uniquely identifies a channel you can be very sure that it is of the type you assert, for iteration the guarding is the preferred approach)

See the typescript guidebook on the topic for more information

btw i did ask in the discord, i just didn't get any replies after asking

let myChannel = '123213123';
(client.channels.cache.get("someTextChannelID")as TextChannel).send("Some message");

Yolo approch work for me...
else make js file ...

This is expected behavior, the caches can hold any channels, channels of type voice or category will of course not have the #send method implemented, after all these channel types can not receive messages as per api specification (and common sense).

You should either type guard or assert (since the ID you provide as key to the #get call uniquely identifies a channel you can be very sure that it is of the type you assert, for iteration the guarding is the preferred approach)

See the typescript guidebook on the topic for more information

if only it was clear in the documentation, we are not reaching the discord ... https://discord.js.org/#/docs/main/stable/class/Channel there is no link for TextChannel or VoiceChannel .... Private way look like silly solution for everyone ... I'm searching 3 days on stack overflow, no one have this simply solution .... We use tha library, we don't suppose to know how the application works completly.

You possibly in the code make accessible method "send" on type Channel and verify that the type can be Textchannel else throw error and explain that textChannel only support sending message.

Discordjs is really opposite of intuitive on typescript ....

That will no close the issue for me because silly option is activate : / .

If you think this needs further discussion or treatment please make a separate issue as enhancement suggestion. This is not a bug, but expected behavior.

If you choose to do so please try to use more constructive language and leave out all the venting and insulting.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Lombra picture Lombra  Â·  3Comments

ghost picture ghost  Â·  3Comments

shukriadams picture shukriadams  Â·  3Comments

tom-barnes picture tom-barnes  Â·  3Comments

ghost picture ghost  Â·  3Comments