From 49f9a1802023d89fc16c7f7f521742f952cfa095 Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Tue, 14 Dec 2021 13:03:39 -0500 Subject: [PATCH] types: use mapped enums instead of overloads (#7088) --- typings/index.d.ts | 91 +++++++++++++++++++++++++---------------- typings/index.test-d.ts | 13 +++++- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index cc8d02d83..6332d46c2 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -461,30 +461,49 @@ export class ButtonInteraction extends Mes public inRawGuild(): this is ButtonInteraction<'raw'>; } +export type KeyedEnum = { + [Key in keyof K]: T | string; +}; + +export type EnumValueMapped, T extends Partial>> = T & { + [Key in keyof T as E[Key]]: T[Key]; +}; + +export type MappedChannelCategoryTypes = EnumValueMapped< + typeof ChannelTypes, + { + GUILD_NEWS: NewsChannel; + GUILD_VOICE: VoiceChannel; + GUILD_TEXT: TextChannel; + GUILD_STORE: StoreChannel; + GUILD_STAGE_VOICE: StageChannel; + } +>; + +export type CategoryChannelTypes = ExcludeEnum< + typeof ChannelTypes, + | 'DM' + | 'GROUP_DM' + | 'UNKNOWN' + | 'GUILD_PUBLIC_THREAD' + | 'GUILD_NEWS_THREAD' + | 'GUILD_PRIVATE_THREAD' + | 'GUILD_CATEGORY' +>; + export class CategoryChannel extends GuildChannel { public readonly children: Collection; public type: 'GUILD_CATEGORY'; - public createChannel( - name: string, - options: CategoryCreateChannelOptions & { type: 'GUILD_VOICE' }, - ): Promise; - public createChannel( - name: string, - options?: CategoryCreateChannelOptions & { type?: 'GUILD_TEXT' }, - ): Promise; - public createChannel( - name: string, - options: CategoryCreateChannelOptions & { type: 'GUILD_NEWS' }, - ): Promise; /** @deprecated See [Self-serve Game Selling Deprecation](https://support-dev.discord.com/hc/en-us/articles/4414590563479) for more information */ public createChannel( name: string, options: CategoryCreateChannelOptions & { type: 'GUILD_STORE' }, ): Promise; - public createChannel( + public createChannel( name: string, - options: CategoryCreateChannelOptions & { type: 'GUILD_STAGE_VOICE' }, - ): Promise; + options: CategoryCreateChannelOptions & { type: T }, + ): Promise; + public createChannel( name: string, options: CategoryCreateChannelOptions, @@ -1379,17 +1398,16 @@ export interface StringMappedInteractionTypes; } -export interface EnumMappedInteractionTypes { - 1: MessageComponentInteraction; - 2: ButtonInteraction; - 3: SelectMenuInteraction; -} - export type WrapBooleanCache = If; -export type MappedInteractionTypes = StringMappedInteractionTypes< - WrapBooleanCache -> & - EnumMappedInteractionTypes>; + +export type MappedInteractionTypes = EnumValueMapped< + typeof MessageComponentTypes, + { + BUTTON: ButtonInteraction>; + SELECT_MENU: SelectMenuInteraction>; + ACTION_ROW: MessageComponentInteraction>; + } +>; export class Message extends Base { private readonly _cacheType: Cached; @@ -2830,6 +2848,16 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager>; } +export type MappedGuildChannelTypes = EnumValueMapped< + typeof ChannelTypes, + { + GUILD_CATEGORY: CategoryChannel; + } +> & + MappedChannelCategoryTypes; + +export type GuildChannelTypes = CategoryChannelTypes | ChannelTypes.GUILD_CATEGORY | 'GUILD_CATEGORY'; + export class GuildChannelManager extends CachedManager< Snowflake, GuildChannel | ThreadChannel, @@ -2838,19 +2866,12 @@ export class GuildChannelManager extends CachedManager< private constructor(guild: Guild, iterable?: Iterable); public readonly channelCountWithoutThreads: number; public guild: Guild; - public create(name: string, options: GuildChannelCreateOptions & { type: 'GUILD_VOICE' }): Promise; - public create( - name: string, - options: GuildChannelCreateOptions & { type: 'GUILD_CATEGORY' }, - ): Promise; - public create(name: string, options?: GuildChannelCreateOptions & { type?: 'GUILD_TEXT' }): Promise; - public create(name: string, options: GuildChannelCreateOptions & { type: 'GUILD_NEWS' }): Promise; /** @deprecated See [Self-serve Game Selling Deprecation](https://support-dev.discord.com/hc/en-us/articles/4414590563479) for more information */ public create(name: string, options: GuildChannelCreateOptions & { type: 'GUILD_STORE' }): Promise; - public create( + public create( name: string, - options: GuildChannelCreateOptions & { type: 'GUILD_STAGE_VOICE' }, - ): Promise; + options: GuildChannelCreateOptions & { type: T }, + ): Promise; public create( name: string, options: GuildChannelCreateOptions, diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index e8a202c0b..9d40205d4 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -756,7 +756,6 @@ declare const threadChannel: ThreadChannel; declare const newsChannel: NewsChannel; declare const textChannel: TextChannel; declare const storeChannel: StoreChannel; -declare const categoryChannel: CategoryChannel; declare const voiceChannel: VoiceChannel; declare const guild: Guild; declare const user: User; @@ -848,6 +847,18 @@ expectType>>(guildApplicationC expectType>>(guildApplicationCommandManager.fetch(undefined, {})); expectType>(guildApplicationCommandManager.fetch('0')); +declare const categoryChannel: CategoryChannel; +{ + expectType>(categoryChannel.createChannel('name', { type: 'GUILD_VOICE' })); + expectType>(categoryChannel.createChannel('name', { type: 'GUILD_TEXT' })); + expectType>(categoryChannel.createChannel('name', { type: 'GUILD_NEWS' })); + expectDeprecated(categoryChannel.createChannel('name', { type: 'GUILD_STORE' })); + expectType>(categoryChannel.createChannel('name', { type: 'GUILD_STAGE_VOICE' })); + expectType>( + categoryChannel.createChannel('name', {}), + ); +} + declare const guildChannelManager: GuildChannelManager; { type AnyChannel = TextChannel | VoiceChannel | CategoryChannel | NewsChannel | StoreChannel | StageChannel;