Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make types more specific and useful for type narrowing #767

Open
Runi-c opened this issue May 12, 2023 · 0 comments
Open

Make types more specific and useful for type narrowing #767

Runi-c opened this issue May 12, 2023 · 0 comments

Comments

@Runi-c
Copy link

Runi-c commented May 12, 2023

Feature

There's quite a variety of places where the types are less useful than they could be.

Some examples:


const reaction: GatewayMessageReactionAddDispatchData = ...;
if(reaction.member?.user.bot) return; //error!

Error: 'reaction.member.user' is possibly 'undefined'.ts(18048)
Problem: The user property is only omitted in message creates & updates according to the docs. This is guaranteed by the docs, so we could make this more accurate.


const channels: RESTGetAPIGuildChannelsResult = await ...;
for(const channel of channels) {
  if(channel.parent_id) { //error!

Error: Property 'parent_id' does not exist on type 'APIGroupDMChannel'.ts(2339)
Problem: We know the channels returned from GET Guild Channels won't be DM channels.


const message: GatewayMessageCreateDispatchData = ...;
if(message.guild_id && !message.author.bot) {
  const roles = message.member.roles; //error!
}

Error: 'message.member' is possibly 'undefined'.ts(18048)
Problem: Discord's docs guarantee that user-generated messages have a valid member from MESSAGE_CREATE events. Even when we narrow the message to only user-generated messages - as is extremely common for bots - we still don't get any help from the types for stuff like this.


Ideal solution or implementation

Where possible, we can make the types from the REST API and from the gateway more useful by creating and choosing narrower types to provide the most information we can for Typescript.

If this is wanted, I'd be willing to whip up a PR based on derived types I've already written for my app.

Alternative solutions or implementations

No response

Other context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants