Skip to content

Make types more specific and useful for type narrowing #767

Open
@Runi-c

Description

@Runi-c

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

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