Skip to content

Commit

Permalink
types: Use readonly arrays and const type parameters in places (#…
Browse files Browse the repository at this point in the history
…9641)

* types: wrap arrays in `Readonly<>`

* refactor: prefer `readonly` syntax

Co-authored-by: Almeida <almeidx@pm.me>

* types: `const` type parameters

* test: add tests

* fix: fix more instances

---------

Co-authored-by: Almeida <almeidx@pm.me>
  • Loading branch information
Jiralite and almeidx authored Jun 12, 2023
1 parent 6c2242f commit cd69868
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 29 deletions.
59 changes: 32 additions & 27 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
public getSubcommandGroup(required?: boolean): string | null;
public getBoolean(name: string, required: true): boolean;
public getBoolean(name: string, required?: boolean): boolean | null;
public getChannel<T extends ChannelType = ChannelType>(
public getChannel<const T extends ChannelType = ChannelType>(
name: string,
required: true,
channelTypes?: T[],
channelTypes?: readonly T[],
): Extract<
NonNullable<CommandInteractionOption<Cached>['channel']>,
{
Expand All @@ -1177,10 +1177,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
: T;
}
>;
public getChannel<T extends ChannelType = ChannelType>(
public getChannel<const T extends ChannelType = ChannelType>(
name: string,
required?: boolean,
channelTypes?: T[],
channelTypes?: readonly T[],
): Extract<
NonNullable<CommandInteractionOption<Cached>['channel']>,
{
Expand Down Expand Up @@ -3713,9 +3713,11 @@ export class ApplicationCommandManager<
id?: Snowflake,
options?: FetchApplicationCommandOptions,
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public set(
commands: ApplicationCommandDataResolvable[],
commands: readonly ApplicationCommandDataResolvable[],
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public set(
commands: readonly ApplicationCommandDataResolvable[],
guildId: Snowflake,
): Promise<Collection<Snowflake, ApplicationCommand>>;
private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody;
Expand Down Expand Up @@ -3748,21 +3750,21 @@ export class ApplicationCommandPermissionsManager<
options:
| (FetchSingleOptions & {
token: string;
channels?: (GuildChannelResolvable | ChannelPermissionConstant)[];
roles?: (RoleResolvable | RolePermissionConstant)[];
users: UserResolvable[];
channels?: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
roles?: readonly (RoleResolvable | RolePermissionConstant)[];
users: readonly UserResolvable[];
})
| (FetchSingleOptions & {
token: string;
channels?: (GuildChannelResolvable | ChannelPermissionConstant)[];
roles: (RoleResolvable | RolePermissionConstant)[];
users?: UserResolvable[];
channels?: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
roles: readonly (RoleResolvable | RolePermissionConstant)[];
users?: readonly UserResolvable[];
})
| (FetchSingleOptions & {
token: string;
channels: (GuildChannelResolvable | ChannelPermissionConstant)[];
roles?: (RoleResolvable | RolePermissionConstant)[];
users?: UserResolvable[];
channels: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
roles?: readonly (RoleResolvable | RolePermissionConstant)[];
users?: readonly UserResolvable[];
}),
): Promise<ApplicationCommandPermissions[]>;
public set(
Expand Down Expand Up @@ -4321,7 +4323,7 @@ export interface ChatInputApplicationCommandData extends BaseApplicationCommandD
description: string;
descriptionLocalizations?: LocalizationMap;
type?: ApplicationCommandType.ChatInput;
options?: ApplicationCommandOptionData[];
options?: readonly ApplicationCommandOptionData[];
}

export type ApplicationCommandData =
Expand All @@ -4331,13 +4333,13 @@ export type ApplicationCommandData =

export interface ApplicationCommandChannelOptionData extends BaseApplicationCommandOptionsData {
type: CommandOptionChannelResolvableType;
channelTypes?: ApplicationCommandOptionAllowedChannelTypes[];
channel_types?: ApplicationCommandOptionAllowedChannelTypes[];
channelTypes?: readonly ApplicationCommandOptionAllowedChannelTypes[];
channel_types?: readonly ApplicationCommandOptionAllowedChannelTypes[];
}

export interface ApplicationCommandChannelOption extends BaseApplicationCommandOptionsData {
type: ApplicationCommandOptionType.Channel;
channelTypes?: ApplicationCommandOptionAllowedChannelTypes[];
channelTypes?: readonly ApplicationCommandOptionAllowedChannelTypes[];
}

export interface ApplicationCommandRoleOptionData extends BaseApplicationCommandOptionsData {
Expand Down Expand Up @@ -4407,14 +4409,14 @@ export interface ApplicationCommandAutocompleteStringOptionData
export interface ApplicationCommandChoicesData<Type extends string | number = string | number>
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoiceData<Type>[];
choices?: readonly ApplicationCommandOptionChoiceData<Type>[];
autocomplete?: false;
}

export interface ApplicationCommandChoicesOption<Type extends string | number = string | number>
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoiceData<Type>[];
choices?: readonly ApplicationCommandOptionChoiceData<Type>[];
autocomplete?: false;
}

Expand Down Expand Up @@ -4456,22 +4458,25 @@ export interface ApplicationCommandBooleanOption extends BaseApplicationCommandO

export interface ApplicationCommandSubGroupData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.SubcommandGroup;
options: ApplicationCommandSubCommandData[];
options: readonly ApplicationCommandSubCommandData[];
}

export interface ApplicationCommandSubGroup extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.SubcommandGroup;
options?: ApplicationCommandSubCommand[];
options?: readonly ApplicationCommandSubCommand[];
}

export interface ApplicationCommandSubCommandData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.Subcommand;
options?: Exclude<ApplicationCommandOptionData, ApplicationCommandSubGroupData | ApplicationCommandSubCommandData>[];
options?: readonly Exclude<
ApplicationCommandOptionData,
ApplicationCommandSubGroupData | ApplicationCommandSubCommandData
>[];
}

export interface ApplicationCommandSubCommand extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.Subcommand;
options?: Exclude<ApplicationCommandOption, ApplicationCommandSubGroup | ApplicationCommandSubCommand>[];
options?: readonly Exclude<ApplicationCommandOption, ApplicationCommandSubGroup | ApplicationCommandSubCommand>[];
}

export interface ApplicationCommandNonOptionsData extends BaseApplicationCommandOptionsData {
Expand Down Expand Up @@ -4527,11 +4532,11 @@ export interface ApplicationCommandPermissionsUpdateData {
id: Snowflake;
guildId: Snowflake;
applicationId: Snowflake;
permissions: ApplicationCommandPermissions[];
permissions: readonly ApplicationCommandPermissions[];
}

export interface EditApplicationCommandPermissionsMixin {
permissions: ApplicationCommandPermissions[];
permissions: readonly ApplicationCommandPermissions[];
token: string;
}

Expand Down
70 changes: 68 additions & 2 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ import {
ThreadManager,
FetchedThreads,
FetchedThreadsMore,
ApplicationCommandChannelOptionData,
ApplicationCommandChannelOption,
ApplicationCommandChoicesOption,
ApplicationCommandChoicesData,
ApplicationCommandSubGroup,
ApplicationCommandSubCommand,
ChatInputApplicationCommandData,
ApplicationCommandPermissionsManager,
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -1355,7 +1363,7 @@ declare const applicationCommandManager: ApplicationCommandManager;
applicationCommandManager.set([applicationCommandData]),
);
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(
applicationCommandManager.set([applicationCommandData], '0'),
applicationCommandManager.set([applicationCommandData] as const, '0'),
);

// Test inference of choice values.
Expand All @@ -1377,6 +1385,41 @@ declare const applicationCommandManager: ApplicationCommandManager;
}
}

declare const applicationCommandPermissionsManager: ApplicationCommandPermissionsManager<
{},
{},
Guild | null,
Snowflake
>;
{
applicationCommandPermissionsManager.add({ permissions: [], token: '' });
applicationCommandPermissionsManager.add({ permissions: [] as const, token: '' });
applicationCommandPermissionsManager.set({ permissions: [], token: '' });
applicationCommandPermissionsManager.set({ permissions: [] as const, token: '' });
applicationCommandPermissionsManager.remove({ channels: [], roles: [], users: [], token: '' });

applicationCommandPermissionsManager.remove({
channels: [] as const,
roles: [] as const,
users: [] as const,
token: '',
});
}

declare const chatInputApplicationCommandData: ChatInputApplicationCommandData;
{
chatInputApplicationCommandData.options = [];
chatInputApplicationCommandData.options = [] as const;
}

declare const applicationCommandChannelOptionData: ApplicationCommandChannelOptionData;
declare const applicationCommandChannelOption: ApplicationCommandChannelOption;
{
applicationCommandChannelOptionData.channelTypes = [] as const;
applicationCommandChannelOptionData.channel_types = [] as const;
applicationCommandChannelOption.channelTypes = [] as const;
}

declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
type: CommandOptionNonChoiceResolvableType;
};
Expand All @@ -1387,10 +1430,32 @@ declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
applicationNonChoiceOptionData.choices;
}

declare const applicationCommandChoicesData: ApplicationCommandChoicesData;
declare const applicationCommandChoicesOption: ApplicationCommandChoicesOption;
{
applicationCommandChoicesData.choices = [];
applicationCommandChoicesData.choices = [] as const;
applicationCommandChoicesOption.choices = [];
applicationCommandChoicesOption.choices = [] as const;
}

declare const applicationCommandSubCommandData: ApplicationCommandSubCommandData;
declare const applicationCommandSubCommand: ApplicationCommandSubCommand;
{
applicationCommandSubCommandData.options = [];
applicationCommandSubCommandData.options = [] as const;
applicationCommandSubCommand.options = [];
applicationCommandSubCommand.options = [] as const;
}

declare const applicationSubGroupCommandData: ApplicationCommandSubGroupData;
declare const applicationCommandSubGroup: ApplicationCommandSubGroup;
{
expectType<ApplicationCommandOptionType.SubcommandGroup>(applicationSubGroupCommandData.type);
expectType<ApplicationCommandSubCommandData[]>(applicationSubGroupCommandData.options);
applicationSubGroupCommandData.options = [];
applicationSubGroupCommandData.options = [] as const;
applicationCommandSubGroup.options = [];
applicationCommandSubGroup.options = [] as const;
}

declare const autoModerationRuleManager: AutoModerationRuleManager;
Expand Down Expand Up @@ -1814,6 +1879,7 @@ client.on('interactionCreate', async interaction => {
expectType<ForumChannel | VoiceChannel>(
interaction.options.getChannel('test', true, [ChannelType.GuildForum, ChannelType.GuildVoice]),
);
expectType<TextChannel>(interaction.options.getChannel('test', true, [ChannelType.GuildText] as const));
expectType<ForumChannel | VoiceChannel | null>(
interaction.options.getChannel('test', false, [ChannelType.GuildForum, ChannelType.GuildVoice]),
);
Expand Down

0 comments on commit cd69868

Please sign in to comment.