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 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']>,
{
@@ -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']>,
{
@@ -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;
@@ -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(
@@ -4321,7 +4323,7 @@ export interface ChatInputApplicationCommandData extends BaseApplicationCommandD
description: string;
descriptionLocalizations?: LocalizationMap;
type?: ApplicationCommandType.ChatInput;
options?: ApplicationCommandOptionData[];
options?: readonly ApplicationCommandOptionData[];
}
export type ApplicationCommandData =
@@ -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 {
@@ -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;
}
@@ -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 {
@@ -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;
}

View File

@@ -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';
@@ -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.
@@ -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;
};
@@ -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;
@@ -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]),
);