Discord.js: TS2339: Property 'send' does not exist on type 'Channel'.

Created on 13 Dec 2019  路  12Comments  路  Source: discordjs/discord.js

Just installed this lib and I'm getting TS2339: Property 'send' does not exist on type 'Channel'. I guess you need to update the types.

question (please use Discord instead) typings

Most helpful comment

The Channel class does not have a send method, because categories and voice channels are also a child classes of the Channel class. When you're certain you have a text channel you can cast it to a TextChannel like so:

(channel as TextChannel).send('hello!');

You have to import TextChannel from the discord.js library for it to work.

For further questions please come by the official Discord server.

All 12 comments

And that is completely correct. Here are the docs.

Since CategoryChannel, TextChannel, VoiceChannel and other types extend GuildChannel, which itself extends Channel, there is no way there will be send for all of them. You can't send messages to voice channels nor categories.

The Channel class does not have a send method, because categories and voice channels are also a child classes of the Channel class. When you're certain you have a text channel you can cast it to a TextChannel like so:

(channel as TextChannel).send('hello!');

You have to import TextChannel from the discord.js library for it to work.

For further questions please come by the official Discord server.

the tutorial says this: https://discordjs.guide/popular-topics/common-questions.html#how-do-i-send-a-message-to-a-certain-channel I guess the tutorial is wrong in this case.

I think the casting solution is just a hack, not a proper solution or I am wrong?

Tutorial is written for JavaScript, not TypeScript.

And no, it's not a hack. You need to tell TypeScript what exactly is the thing you want to work on. Since literally every channel type is a Channel, it resolves to that as a default, and it will always be true. This is how you specify type of something.

then you have no type safety.

The proper way to do it, would be like this:

if(channel.type === ChannelType.text){
   channel.send(`bla`)
}

your channel will be of type: TextChannel|CategoryChannel|VoiceChannel

Casting is a hack because loses all the benefits of typing.

Would be lovely if you gave more context.

For instance, accessing the channel property from a message results in a TextBasedChannel, which does have the send method.
This is the case for a lot of other situations, so I'm assuming you're running get on a ChannelStore/Collection, in which case, there's no way for d.js to tell the type of the channel.

Also, you never mentioned the version you're on, master had its typings pretty much refactored, and they are much more accurate, however, those changes will not be backported to any other version.

im on the 12.0.0-dev version. What context would you need?

I'm basically trying to follow the guide from here:

const channel = <client>.channels.get('<id>');
channel.send('<content>');

wonderful, and how do you want us to figure out the type of that channel again?
<client>.channels is a ChannelStore, which extends DataStore.. which extends Collection, which... extends the nodejs Map

<client>.channels ends up being a Map<string, Channel>

so anyway, if you think you have a solution feel free to PR, but spoiler alert, there's nothing you can do about it, that isnt "hacky", so for that blessed get method you'll simply have to use as all the time.

Discord.js new version doesnt working

@didinele as i've said there is no way to put a key called type in the channel you return?

Discord.js new version doesnt working

Please be either more specific, if it has anything to do with this specific topic, or, preferably come to the discord server: https://discord.gg/bRCvFy9
This issue tracker is only for bug reports and enhancement suggestions. You won't receive any basic help here.

No, there isn't a way, we have a map, the element returned cannot be determined inside of the typings.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Blumlaut picture Blumlaut  路  3Comments

Lombra picture Lombra  路  3Comments

Dmitry221060 picture Dmitry221060  路  3Comments

LLamaFTL picture LLamaFTL  路  3Comments

iCrawl picture iCrawl  路  3Comments