Discord.js: The ChannelManager.fetch() method does not return an object of type Channel.

Created on 9 Sep 2020  路  3Comments  路  Source: discordjs/discord.js

Hi!

I am testing discord.js to interact on my server with my bot.
When wanting to recover a channel and use the characteristics of typescript, I realized that the return type in the .fetch method of the ChannelManager object is wrong.
The official documentation indicates that it returns an object of type Channel, but it is not what really happens.
no return channel

This error prevents us from declaring the type of object received and then accessing its properties.

The following type declaration returns a typescript error:

// Fetch a channel by its id
const channel: TextChannel = await this.discordClient.channels.fetch('CHANNEL_ID');

error_typescript
error_props

Changing the return type would fix this error?
https://github.com/discordjs/discord.js/blob/56e8ef2d38633154540748acb64f4c1305fcc6d3/src/managers/ChannelManager.js#L78

Thanks!

question (please use Discord instead)

Most helpful comment

Channel is the common ancestor of all types which can be fetched using that method. However tighter inference of types is not possible due to some quirks in the way the classes are written (which is in JS, not TS)

To use this with TS you'll need to force typecasting with as TextChannel or similar.

All 3 comments

Channel is the common ancestor of all types which can be fetched using that method. However tighter inference of types is not possible due to some quirks in the way the classes are written (which is in JS, not TS)

To use this with TS you'll need to force typecasting with as TextChannel or similar.

If you need help with discord.js installation or usage, please go to the discord.js Discord server instead:
https://discord.gg/bRCvFy9
This issue tracker is only for bug reports and enhancement suggestions.

If you need help with discord.js installation or usage, please go to the discord.js Discord server instead:
https://discord.gg/bRCvFy9
This issue tracker is only for bug reports and enhancement suggestions.

I posted here because I thought it was a mistake.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tiritto picture tiritto  路  3Comments

Brawaru picture Brawaru  路  3Comments

smchase picture smchase  路  3Comments

Alipoodle picture Alipoodle  路  3Comments

iCrawl picture iCrawl  路  3Comments