Skip to content

Commit cd69868

Browse files
Jiralitealmeidx
andauthored
types: Use readonly arrays and const type parameters in places (#9641)
* types: wrap arrays in `Readonly<>` * refactor: prefer `readonly` syntax Co-authored-by: Almeida <[email protected]> * types: `const` type parameters * test: add tests * fix: fix more instances --------- Co-authored-by: Almeida <[email protected]>
1 parent 6c2242f commit cd69868

File tree

2 files changed

+100
-29
lines changed

2 files changed

+100
-29
lines changed

packages/discord.js/typings/index.d.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
11621162
public getSubcommandGroup(required?: boolean): string | null;
11631163
public getBoolean(name: string, required: true): boolean;
11641164
public getBoolean(name: string, required?: boolean): boolean | null;
1165-
public getChannel<T extends ChannelType = ChannelType>(
1165+
public getChannel<const T extends ChannelType = ChannelType>(
11661166
name: string,
11671167
required: true,
1168-
channelTypes?: T[],
1168+
channelTypes?: readonly T[],
11691169
): Extract<
11701170
NonNullable<CommandInteractionOption<Cached>['channel']>,
11711171
{
@@ -1177,10 +1177,10 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
11771177
: T;
11781178
}
11791179
>;
1180-
public getChannel<T extends ChannelType = ChannelType>(
1180+
public getChannel<const T extends ChannelType = ChannelType>(
11811181
name: string,
11821182
required?: boolean,
1183-
channelTypes?: T[],
1183+
channelTypes?: readonly T[],
11841184
): Extract<
11851185
NonNullable<CommandInteractionOption<Cached>['channel']>,
11861186
{
@@ -3713,9 +3713,11 @@ export class ApplicationCommandManager<
37133713
id?: Snowflake,
37143714
options?: FetchApplicationCommandOptions,
37153715
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
3716-
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommandScope>>;
37173716
public set(
3718-
commands: ApplicationCommandDataResolvable[],
3717+
commands: readonly ApplicationCommandDataResolvable[],
3718+
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
3719+
public set(
3720+
commands: readonly ApplicationCommandDataResolvable[],
37193721
guildId: Snowflake,
37203722
): Promise<Collection<Snowflake, ApplicationCommand>>;
37213723
private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody;
@@ -3748,21 +3750,21 @@ export class ApplicationCommandPermissionsManager<
37483750
options:
37493751
| (FetchSingleOptions & {
37503752
token: string;
3751-
channels?: (GuildChannelResolvable | ChannelPermissionConstant)[];
3752-
roles?: (RoleResolvable | RolePermissionConstant)[];
3753-
users: UserResolvable[];
3753+
channels?: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
3754+
roles?: readonly (RoleResolvable | RolePermissionConstant)[];
3755+
users: readonly UserResolvable[];
37543756
})
37553757
| (FetchSingleOptions & {
37563758
token: string;
3757-
channels?: (GuildChannelResolvable | ChannelPermissionConstant)[];
3758-
roles: (RoleResolvable | RolePermissionConstant)[];
3759-
users?: UserResolvable[];
3759+
channels?: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
3760+
roles: readonly (RoleResolvable | RolePermissionConstant)[];
3761+
users?: readonly UserResolvable[];
37603762
})
37613763
| (FetchSingleOptions & {
37623764
token: string;
3763-
channels: (GuildChannelResolvable | ChannelPermissionConstant)[];
3764-
roles?: (RoleResolvable | RolePermissionConstant)[];
3765-
users?: UserResolvable[];
3765+
channels: readonly (GuildChannelResolvable | ChannelPermissionConstant)[];
3766+
roles?: readonly (RoleResolvable | RolePermissionConstant)[];
3767+
users?: readonly UserResolvable[];
37663768
}),
37673769
): Promise<ApplicationCommandPermissions[]>;
37683770
public set(
@@ -4321,7 +4323,7 @@ export interface ChatInputApplicationCommandData extends BaseApplicationCommandD
43214323
description: string;
43224324
descriptionLocalizations?: LocalizationMap;
43234325
type?: ApplicationCommandType.ChatInput;
4324-
options?: ApplicationCommandOptionData[];
4326+
options?: readonly ApplicationCommandOptionData[];
43254327
}
43264328

43274329
export type ApplicationCommandData =
@@ -4331,13 +4333,13 @@ export type ApplicationCommandData =
43314333

43324334
export interface ApplicationCommandChannelOptionData extends BaseApplicationCommandOptionsData {
43334335
type: CommandOptionChannelResolvableType;
4334-
channelTypes?: ApplicationCommandOptionAllowedChannelTypes[];
4335-
channel_types?: ApplicationCommandOptionAllowedChannelTypes[];
4336+
channelTypes?: readonly ApplicationCommandOptionAllowedChannelTypes[];
4337+
channel_types?: readonly ApplicationCommandOptionAllowedChannelTypes[];
43364338
}
43374339

43384340
export interface ApplicationCommandChannelOption extends BaseApplicationCommandOptionsData {
43394341
type: ApplicationCommandOptionType.Channel;
4340-
channelTypes?: ApplicationCommandOptionAllowedChannelTypes[];
4342+
channelTypes?: readonly ApplicationCommandOptionAllowedChannelTypes[];
43414343
}
43424344

43434345
export interface ApplicationCommandRoleOptionData extends BaseApplicationCommandOptionsData {
@@ -4407,14 +4409,14 @@ export interface ApplicationCommandAutocompleteStringOptionData
44074409
export interface ApplicationCommandChoicesData<Type extends string | number = string | number>
44084410
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
44094411
type: CommandOptionChoiceResolvableType;
4410-
choices?: ApplicationCommandOptionChoiceData<Type>[];
4412+
choices?: readonly ApplicationCommandOptionChoiceData<Type>[];
44114413
autocomplete?: false;
44124414
}
44134415

44144416
export interface ApplicationCommandChoicesOption<Type extends string | number = string | number>
44154417
extends Omit<BaseApplicationCommandOptionsData, 'autocomplete'> {
44164418
type: CommandOptionChoiceResolvableType;
4417-
choices?: ApplicationCommandOptionChoiceData<Type>[];
4419+
choices?: readonly ApplicationCommandOptionChoiceData<Type>[];
44184420
autocomplete?: false;
44194421
}
44204422

@@ -4456,22 +4458,25 @@ export interface ApplicationCommandBooleanOption extends BaseApplicationCommandO
44564458

44574459
export interface ApplicationCommandSubGroupData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
44584460
type: ApplicationCommandOptionType.SubcommandGroup;
4459-
options: ApplicationCommandSubCommandData[];
4461+
options: readonly ApplicationCommandSubCommandData[];
44604462
}
44614463

44624464
export interface ApplicationCommandSubGroup extends Omit<BaseApplicationCommandOptionsData, 'required'> {
44634465
type: ApplicationCommandOptionType.SubcommandGroup;
4464-
options?: ApplicationCommandSubCommand[];
4466+
options?: readonly ApplicationCommandSubCommand[];
44654467
}
44664468

44674469
export interface ApplicationCommandSubCommandData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
44684470
type: ApplicationCommandOptionType.Subcommand;
4469-
options?: Exclude<ApplicationCommandOptionData, ApplicationCommandSubGroupData | ApplicationCommandSubCommandData>[];
4471+
options?: readonly Exclude<
4472+
ApplicationCommandOptionData,
4473+
ApplicationCommandSubGroupData | ApplicationCommandSubCommandData
4474+
>[];
44704475
}
44714476

44724477
export interface ApplicationCommandSubCommand extends Omit<BaseApplicationCommandOptionsData, 'required'> {
44734478
type: ApplicationCommandOptionType.Subcommand;
4474-
options?: Exclude<ApplicationCommandOption, ApplicationCommandSubGroup | ApplicationCommandSubCommand>[];
4479+
options?: readonly Exclude<ApplicationCommandOption, ApplicationCommandSubGroup | ApplicationCommandSubCommand>[];
44754480
}
44764481

44774482
export interface ApplicationCommandNonOptionsData extends BaseApplicationCommandOptionsData {
@@ -4527,11 +4532,11 @@ export interface ApplicationCommandPermissionsUpdateData {
45274532
id: Snowflake;
45284533
guildId: Snowflake;
45294534
applicationId: Snowflake;
4530-
permissions: ApplicationCommandPermissions[];
4535+
permissions: readonly ApplicationCommandPermissions[];
45314536
}
45324537

45334538
export interface EditApplicationCommandPermissionsMixin {
4534-
permissions: ApplicationCommandPermissions[];
4539+
permissions: readonly ApplicationCommandPermissions[];
45354540
token: string;
45364541
}
45374542

packages/discord.js/typings/index.test-d.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ import {
163163
ThreadManager,
164164
FetchedThreads,
165165
FetchedThreadsMore,
166+
ApplicationCommandChannelOptionData,
167+
ApplicationCommandChannelOption,
168+
ApplicationCommandChoicesOption,
169+
ApplicationCommandChoicesData,
170+
ApplicationCommandSubGroup,
171+
ApplicationCommandSubCommand,
172+
ChatInputApplicationCommandData,
173+
ApplicationCommandPermissionsManager,
166174
} from '.';
167175
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
168176
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
@@ -1355,7 +1363,7 @@ declare const applicationCommandManager: ApplicationCommandManager;
13551363
applicationCommandManager.set([applicationCommandData]),
13561364
);
13571365
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(
1358-
applicationCommandManager.set([applicationCommandData], '0'),
1366+
applicationCommandManager.set([applicationCommandData] as const, '0'),
13591367
);
13601368

13611369
// Test inference of choice values.
@@ -1377,6 +1385,41 @@ declare const applicationCommandManager: ApplicationCommandManager;
13771385
}
13781386
}
13791387

1388+
declare const applicationCommandPermissionsManager: ApplicationCommandPermissionsManager<
1389+
{},
1390+
{},
1391+
Guild | null,
1392+
Snowflake
1393+
>;
1394+
{
1395+
applicationCommandPermissionsManager.add({ permissions: [], token: '' });
1396+
applicationCommandPermissionsManager.add({ permissions: [] as const, token: '' });
1397+
applicationCommandPermissionsManager.set({ permissions: [], token: '' });
1398+
applicationCommandPermissionsManager.set({ permissions: [] as const, token: '' });
1399+
applicationCommandPermissionsManager.remove({ channels: [], roles: [], users: [], token: '' });
1400+
1401+
applicationCommandPermissionsManager.remove({
1402+
channels: [] as const,
1403+
roles: [] as const,
1404+
users: [] as const,
1405+
token: '',
1406+
});
1407+
}
1408+
1409+
declare const chatInputApplicationCommandData: ChatInputApplicationCommandData;
1410+
{
1411+
chatInputApplicationCommandData.options = [];
1412+
chatInputApplicationCommandData.options = [] as const;
1413+
}
1414+
1415+
declare const applicationCommandChannelOptionData: ApplicationCommandChannelOptionData;
1416+
declare const applicationCommandChannelOption: ApplicationCommandChannelOption;
1417+
{
1418+
applicationCommandChannelOptionData.channelTypes = [] as const;
1419+
applicationCommandChannelOptionData.channel_types = [] as const;
1420+
applicationCommandChannelOption.channelTypes = [] as const;
1421+
}
1422+
13801423
declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
13811424
type: CommandOptionNonChoiceResolvableType;
13821425
};
@@ -1387,10 +1430,32 @@ declare const applicationNonChoiceOptionData: ApplicationCommandOptionData & {
13871430
applicationNonChoiceOptionData.choices;
13881431
}
13891432

1433+
declare const applicationCommandChoicesData: ApplicationCommandChoicesData;
1434+
declare const applicationCommandChoicesOption: ApplicationCommandChoicesOption;
1435+
{
1436+
applicationCommandChoicesData.choices = [];
1437+
applicationCommandChoicesData.choices = [] as const;
1438+
applicationCommandChoicesOption.choices = [];
1439+
applicationCommandChoicesOption.choices = [] as const;
1440+
}
1441+
1442+
declare const applicationCommandSubCommandData: ApplicationCommandSubCommandData;
1443+
declare const applicationCommandSubCommand: ApplicationCommandSubCommand;
1444+
{
1445+
applicationCommandSubCommandData.options = [];
1446+
applicationCommandSubCommandData.options = [] as const;
1447+
applicationCommandSubCommand.options = [];
1448+
applicationCommandSubCommand.options = [] as const;
1449+
}
1450+
13901451
declare const applicationSubGroupCommandData: ApplicationCommandSubGroupData;
1452+
declare const applicationCommandSubGroup: ApplicationCommandSubGroup;
13911453
{
13921454
expectType<ApplicationCommandOptionType.SubcommandGroup>(applicationSubGroupCommandData.type);
1393-
expectType<ApplicationCommandSubCommandData[]>(applicationSubGroupCommandData.options);
1455+
applicationSubGroupCommandData.options = [];
1456+
applicationSubGroupCommandData.options = [] as const;
1457+
applicationCommandSubGroup.options = [];
1458+
applicationCommandSubGroup.options = [] as const;
13941459
}
13951460

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

0 commit comments

Comments
 (0)