types: Use readonly arrays and const type parameters in places (#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>
This commit is contained in:
Jiralite
2023-06-12 19:00:45 +01:00
committed by GitHub
parent 6c2242f4f9
commit cd6986854f
2 changed files with 100 additions and 29 deletions

View File

@@ -1162,10 +1162,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
public getSubcommandGroup(required?: boolean): string | null; public getSubcommandGroup(required?: boolean): string | null;
public getBoolean(name: string, required: true): boolean; public getBoolean(name: string, required: true): boolean;
public getBoolean(name: string, required?: boolean): boolean | null; public getBoolean(name: string, required?: boolean): boolean | null;
public getChannel<T extends ChannelType = ChannelType>( public getChannel<const T extends ChannelType = ChannelType>(
name: string, name: string,
required: true, required: true,
channelTypes?: T[], channelTypes?: readonly T[],
): Extract< ): Extract<
NonNullable<CommandInteractionOption<Cached>['channel']>, NonNullable<CommandInteractionOption<Cached>['channel']>,
{ {
@@ -1177,10 +1177,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
: T; : T;
} }
>; >;
public getChannel<T extends ChannelType = ChannelType>( public getChannel<const T extends ChannelType = ChannelType>(
name: string, name: string,
required?: boolean, required?: boolean,
channelTypes?: T[], channelTypes?: readonly T[],
): Extract< ): Extract<
NonNullable<CommandInteractionOption<Cached>['channel']>, NonNullable<CommandInteractionOption<Cached>['channel']>,
{ {
@@ -3713,9 +3713,11 @@ export class ApplicationCommandManager<
id?: Snowflake, id?: Snowflake,
options?: FetchApplicationCommandOptions, options?: FetchApplicationCommandOptions,
): Promise<Collection<Snowflake, ApplicationCommandScope>>; ): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public set( public set(
commands: ApplicationCommandDataResolvable[], commands: readonly ApplicationCommandDataResolvable[],
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public set(
commands: readonly ApplicationCommandDataResolvable[],
guildId: Snowflake, guildId: Snowflake,
): Promise<Collection<Snowflake, ApplicationCommand>>; ): Promise<Collection<Snowflake, ApplicationCommand>>;
private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody; private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody;
@@ -3748,21 +3750,21 @@ export class ApplicationCommandPermissionsManager<
options: options:
| (FetchSingleOptions & { | (FetchSingleOptions & {
token: string; token: string;
channels?: (GuildChannelResolvable | ChannelPermissionConstant)[]; channels?: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
roles?: (RoleResolvable | RolePermissionConstant)[]; roles?: readonly (RoleResolvable | RolePermissionConstant)[];
users: UserResolvable[]; users: readonly UserResolvable[];
}) })
| (FetchSingleOptions & { | (FetchSingleOptions & {
token: string; token: string;
channels?: (GuildChannelResolvable | ChannelPermissionConstant)[]; channels?: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
roles: (RoleResolvable | RolePermissionConstant)[]; roles: readonly (RoleResolvable | RolePermissionConstant)[];
users?: UserResolvable[]; users?: readonly UserResolvable[];
}) })
| (FetchSingleOptions & { | (FetchSingleOptions & {
token: string; token: string;
channels: (GuildChannelResolvable | ChannelPermissionConstant)[]; channels: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
roles?: (RoleResolvable | RolePermissionConstant)[]; roles?: readonly (RoleResolvable | RolePermissionConstant)[];
users?: UserResolvable[]; users?: readonly UserResolvable[];
}), }),
): Promise<ApplicationCommandPermissions[]>; ): Promise<ApplicationCommandPermissions[]>;
public set( public set(
@@ -4321,7 +4323,7 @@ export interface ChatInputApplicationCommandData extends BaseApplicationCommandD
description: string; description: string;
descriptionLocalizations?: LocalizationMap; descriptionLocalizations?: LocalizationMap;
type?: ApplicationCommandType.ChatInput; type?: ApplicationCommandType.ChatInput;
options?: ApplicationCommandOptionData[]; options?: readonly ApplicationCommandOptionData[];
} }
export type ApplicationCommandData = export type ApplicationCommandData =
@@ -4331,13 +4333,13 @@ export type ApplicationCommandData =
export interface ApplicationCommandChannelOptionData extends BaseApplicationCommandOptionsData { export interface ApplicationCommandChannelOptionData extends BaseApplicationCommandOptionsData {
type: CommandOptionChannelResolvableType; type: CommandOptionChannelResolvableType;
channelTypes?: ApplicationCommandOptionAllowedChannelTypes[]; channelTypes?: readonly ApplicationCommandOptionAllowedChannelTypes[];
channel_types?: ApplicationCommandOptionAllowedChannelTypes[]; channel_types?: readonly ApplicationCommandOptionAllowedChannelTypes[];
} }
export interface ApplicationCommandChannelOption extends BaseApplicationCommandOptionsData { export interface ApplicationCommandChannelOption extends BaseApplicationCommandOptionsData {
type: ApplicationCommandOptionType.Channel; type: ApplicationCommandOptionType.Channel;
channelTypes?: ApplicationCommandOptionAllowedChannelTypes[]; channelTypes?: readonly ApplicationCommandOptionAllowedChannelTypes[];
} }
export interface ApplicationCommandRoleOptionData extends BaseApplicationCommandOptionsData { export interface ApplicationCommandRoleOptionData extends BaseApplicationCommandOptionsData {
@@ -4407,14 +4409,14 @@ export interface ApplicationCommandAutocompleteStringOptionData
export interface ApplicationCommandChoicesData<Type extends string | number = string | number> export interface ApplicationCommandChoicesData<Type extends string | number = string | number>
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> { extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType; type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoiceData<Type>[]; choices?: readonly ApplicationCommandOptionChoiceData<Type>[];
autocomplete?: false; autocomplete?: false;
} }
export interface ApplicationCommandChoicesOption<Type extends string | number = string | number> export interface ApplicationCommandChoicesOption<Type extends string | number = string | number>
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> { extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
type: CommandOptionChoiceResolvableType; type: CommandOptionChoiceResolvableType;
choices?: ApplicationCommandOptionChoiceData<Type>[]; choices?: readonly ApplicationCommandOptionChoiceData<Type>[];
autocomplete?: false; autocomplete?: false;
} }
@@ -4456,22 +4458,25 @@ export interface ApplicationCommandBooleanOption extends BaseApplicationCommandO
export interface ApplicationCommandSubGroupData extends Omit<BaseApplicationCommandOptionsData, 'required'> { export interface ApplicationCommandSubGroupData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.SubcommandGroup; type: ApplicationCommandOptionType.SubcommandGroup;
options: ApplicationCommandSubCommandData[]; options: readonly ApplicationCommandSubCommandData[];
} }
export interface ApplicationCommandSubGroup extends Omit<BaseApplicationCommandOptionsData, 'required'> { export interface ApplicationCommandSubGroup extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.SubcommandGroup; type: ApplicationCommandOptionType.SubcommandGroup;
options?: ApplicationCommandSubCommand[]; options?: readonly ApplicationCommandSubCommand[];
} }
export interface ApplicationCommandSubCommandData extends Omit<BaseApplicationCommandOptionsData, 'required'> { export interface ApplicationCommandSubCommandData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.Subcommand; type: ApplicationCommandOptionType.Subcommand;
options?: Exclude<ApplicationCommandOptionData, ApplicationCommandSubGroupData | ApplicationCommandSubCommandData>[]; options?: readonly Exclude<
ApplicationCommandOptionData,
ApplicationCommandSubGroupData | ApplicationCommandSubCommandData
>[];
} }
export interface ApplicationCommandSubCommand extends Omit<BaseApplicationCommandOptionsData, 'required'> { export interface ApplicationCommandSubCommand extends Omit<BaseApplicationCommandOptionsData, 'required'> {
type: ApplicationCommandOptionType.Subcommand; type: ApplicationCommandOptionType.Subcommand;
options?: Exclude<ApplicationCommandOption, ApplicationCommandSubGroup | ApplicationCommandSubCommand>[]; options?: readonly Exclude<ApplicationCommandOption, ApplicationCommandSubGroup | ApplicationCommandSubCommand>[];
} }
export interface ApplicationCommandNonOptionsData extends BaseApplicationCommandOptionsData { export interface ApplicationCommandNonOptionsData extends BaseApplicationCommandOptionsData {
@@ -4527,11 +4532,11 @@ export interface ApplicationCommandPermissionsUpdateData {
id: Snowflake; id: Snowflake;
guildId: Snowflake; guildId: Snowflake;
applicationId: Snowflake; applicationId: Snowflake;
permissions: ApplicationCommandPermissions[]; permissions: readonly ApplicationCommandPermissions[];
} }
export interface EditApplicationCommandPermissionsMixin { export interface EditApplicationCommandPermissionsMixin {
permissions: ApplicationCommandPermissions[]; permissions: readonly ApplicationCommandPermissions[];
token: string; token: string;
} }

View File

@@ -163,6 +163,14 @@ import {
ThreadManager, ThreadManager,
FetchedThreads, FetchedThreads,
FetchedThreadsMore, FetchedThreadsMore,
ApplicationCommandChannelOptionData,
ApplicationCommandChannelOption,
ApplicationCommandChoicesOption,
ApplicationCommandChoicesData,
ApplicationCommandSubGroup,
ApplicationCommandSubCommand,
ChatInputApplicationCommandData,
ApplicationCommandPermissionsManager,
} from '.'; } from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd'; import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders'; import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
@@ -1355,7 +1363,7 @@ declare const applicationCommandManager: ApplicationCommandManager;
applicationCommandManager.set([applicationCommandData]), applicationCommandManager.set([applicationCommandData]),
); );
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>( expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(
applicationCommandManager.set([applicationCommandData], '0'), applicationCommandManager.set([applicationCommandData] as const, '0'),
); );
// Test inference of choice values. // Test inference of choice values.
@@ -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 & { declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
type: CommandOptionNonChoiceResolvableType; type: CommandOptionNonChoiceResolvableType;
}; };
@@ -1387,10 +1430,32 @@ declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
applicationNonChoiceOptionData.choices; 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 applicationSubGroupCommandData: ApplicationCommandSubGroupData;
declare const applicationCommandSubGroup: ApplicationCommandSubGroup;
{ {
expectType<ApplicationCommandOptionType.SubcommandGroup>(applicationSubGroupCommandData.type); 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; declare const autoModerationRuleManager: AutoModerationRuleManager;
@@ -1814,6 +1879,7 @@ client.on('interactionCreate', async interaction => {
expectType<ForumChannel | VoiceChannel>( expectType<ForumChannel | VoiceChannel>(
interaction.options.getChannel('test', true, [ChannelType.GuildForum, ChannelType.GuildVoice]), interaction.options.getChannel('test', true, [ChannelType.GuildForum, ChannelType.GuildVoice]),
); );
expectType<TextChannel>(interaction.options.getChannel('test', true, [ChannelType.GuildText] as const));
expectType<ForumChannel | VoiceChannel | null>( expectType<ForumChannel | VoiceChannel | null>(
interaction.options.getChannel('test', false, [ChannelType.GuildForum, ChannelType.GuildVoice]), interaction.options.getChannel('test', false, [ChannelType.GuildForum, ChannelType.GuildVoice]),
); );