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

types: use mapped enums instead of overloads #7088

Merged
merged 2 commits into from Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 56 additions & 35 deletions typings/index.d.ts
Expand Up @@ -454,30 +454,49 @@ export class ButtonInteraction<Cached extends CacheType = CacheType> extends Mes
public inRawGuild(): this is ButtonInteraction<'raw'>;
}

export type KeyedEnum<K, T> = {
[Key in keyof K]: T | string;
};

export type EnumValueMapped<E extends KeyedEnum<T, number>, T extends Partial<Record<keyof E, unknown>>> = 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<Snowflake, GuildChannel>;
public type: 'GUILD_CATEGORY';
public createChannel(
name: string,
options: CategoryCreateChannelOptions & { type: 'GUILD_VOICE' },
): Promise<VoiceChannel>;
public createChannel(
name: string,
options?: CategoryCreateChannelOptions & { type?: 'GUILD_TEXT' },
): Promise<TextChannel>;
public createChannel(
name: string,
options: CategoryCreateChannelOptions & { type: 'GUILD_NEWS' },
): Promise<NewsChannel>;
/** @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<StoreChannel>;
public createChannel(
public createChannel<T extends CategoryChannelTypes>(
name: string,
options: CategoryCreateChannelOptions & { type: 'GUILD_STAGE_VOICE' },
): Promise<StageChannel>;
options: CategoryCreateChannelOptions & { type: T },
): Promise<MappedChannelCategoryTypes[T]>;

public createChannel(
name: string,
options: CategoryCreateChannelOptions,
Expand Down Expand Up @@ -1370,17 +1389,16 @@ export interface StringMappedInteractionTypes<Cached extends CacheType = CacheTy
ACTION_ROW: MessageComponentInteraction<Cached>;
}

export interface EnumMappedInteractionTypes<Cached extends CacheType = CacheType> {
1: MessageComponentInteraction<Cached>;
2: ButtonInteraction<Cached>;
3: SelectMenuInteraction<Cached>;
}

export type WrapBooleanCache<T extends boolean> = If<T, 'cached', CacheType>;
export type MappedInteractionTypes<Cached extends boolean = boolean> = StringMappedInteractionTypes<
WrapBooleanCache<Cached>
> &
EnumMappedInteractionTypes<WrapBooleanCache<Cached>>;

export type MappedInteractionTypes<Cached extends boolean = boolean> = EnumValueMapped<
typeof MessageComponentTypes,
{
BUTTON: ButtonInteraction<WrapBooleanCache<Cached>>;
SELECT_MENU: SelectMenuInteraction<WrapBooleanCache<Cached>>;
ACTION_ROW: MessageComponentInteraction<WrapBooleanCache<Cached>>;
}
>;

export class Message<Cached extends boolean = boolean> extends Base {
private readonly _cacheType: Cached;
Expand Down Expand Up @@ -2812,6 +2830,16 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommand>>;
}

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,
Expand All @@ -2820,19 +2848,12 @@ export class GuildChannelManager extends CachedManager<
private constructor(guild: Guild, iterable?: Iterable<RawGuildChannelData>);
public readonly channelCountWithoutThreads: number;
public guild: Guild;
public create(name: string, options: GuildChannelCreateOptions & { type: 'GUILD_VOICE' }): Promise<VoiceChannel>;
public create(
name: string,
options: GuildChannelCreateOptions & { type: 'GUILD_CATEGORY' },
): Promise<CategoryChannel>;
public create(name: string, options?: GuildChannelCreateOptions & { type?: 'GUILD_TEXT' }): Promise<TextChannel>;
public create(name: string, options: GuildChannelCreateOptions & { type: 'GUILD_NEWS' }): Promise<NewsChannel>;
/** @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<StoreChannel>;
public create(
public create<T extends GuildChannelTypes>(
name: string,
options: GuildChannelCreateOptions & { type: 'GUILD_STAGE_VOICE' },
): Promise<StageChannel>;
options: GuildChannelCreateOptions & { type: T },
): Promise<MappedGuildChannelTypes[T]>;
public create(
name: string,
options: GuildChannelCreateOptions,
Expand Down
13 changes: 12 additions & 1 deletion typings/index.test-d.ts
Expand Up @@ -752,7 +752,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;
Expand Down Expand Up @@ -844,6 +843,18 @@ expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(guildApplicationC
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(guildApplicationCommandManager.fetch(undefined, {}));
expectType<Promise<ApplicationCommand>>(guildApplicationCommandManager.fetch('0'));

declare const categoryChannel: CategoryChannel;
{
expectType<Promise<VoiceChannel>>(categoryChannel.createChannel('name', { type: 'GUILD_VOICE' }));
expectType<Promise<TextChannel>>(categoryChannel.createChannel('name', { type: 'GUILD_TEXT' }));
expectType<Promise<NewsChannel>>(categoryChannel.createChannel('name', { type: 'GUILD_NEWS' }));
expectDeprecated(categoryChannel.createChannel('name', { type: 'GUILD_STORE' }));
expectType<Promise<StageChannel>>(categoryChannel.createChannel('name', { type: 'GUILD_STAGE_VOICE' }));
expectType<Promise<TextChannel | VoiceChannel | NewsChannel | StoreChannel | StageChannel>>(
categoryChannel.createChannel('name', {}),
);
}

declare const guildChannelManager: GuildChannelManager;
{
type AnyChannel = TextChannel | VoiceChannel | CategoryChannel | NewsChannel | StoreChannel | StageChannel;
Expand Down