Closed john-batch closed 2 years ago
I've actually had the need for this myself and simply hacked it up:
import type { GatewayDispatchEvents, GatewayDispatchPayload, GatewaySendPayload } from 'discord-api-types/v10';
type _DiscordEventsMap = {
[K in GatewayDispatchEvents]: GatewayDispatchPayload & {
t: K;
};
};
export type DiscordEventsMap = {
// @ts-expect-error
[K in keyof _DiscordEventsMap]: _DiscordEventsMap[K]['d'];
} & {
send: GatewaySendPayload;
};
It doesn't play nicely with TSC, given the needed @ts-expect-error
(as of about TS 4.8) and the fact that it needs two types (you'd think you could just inline the first one, but it does not behave the same way), but it gets the job done.
The reason your hack requires @ts-expect-error
is because some events don't have an associated payload (like ApplicationCommandPermissionsUpdate). You can fix it like this :
type _Payload = {
[E in GatewayDispatchEvents]: GatewayDispatchPayload & { t: E }
}
type GatewayDispatchData = {
[E in keyof _Payload]: "d" extends keyof _Payload[E] ? _Payload[E]["d"] : never
}
I edited my original post to include it as an alternative implementation.
Good catch! That explains why it only recently started occurring, thanks.
Thats quite a big issue if there's events without associated payloads... PRs are more than welcome!!
Thats quite a big issue if there's events without associated payloads... PRs are more than welcome!!
Done. (#619)
Feature
A good addition would be an interface to associate a GatewayDispatchEvents event with the relevant DataPayload dispatched along this event.
Ideal solution or implementation
The following interface only works for V10 :
Alternative solutions or implementations
Other context
No response