From 41390e2eec907142184fc5b290e663937dfb90eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=86MB=C3=98?= Date: Sun, 5 May 2024 16:45:02 -0700 Subject: [PATCH 1/4] types(SlashCommandBuilder): add missing shared properties --- .../slashCommands/mixins/SharedSlashCommand.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/builders/src/interactions/slashCommands/mixins/SharedSlashCommand.ts b/packages/builders/src/interactions/slashCommands/mixins/SharedSlashCommand.ts index 4d321073c79c..9e178f82f1f1 100644 --- a/packages/builders/src/interactions/slashCommands/mixins/SharedSlashCommand.ts +++ b/packages/builders/src/interactions/slashCommands/mixins/SharedSlashCommand.ts @@ -27,6 +27,17 @@ export class SharedSlashCommand { public readonly options: ToAPIApplicationCommandOptions[] = []; + /** + * @deprecated Use {@link SharedSlashCommand.setDefaultMemberPermissions} or {@link SharedSlashCommand.setDMPermission} instead. + */ + public readonly default_permission: boolean | undefined = undefined; + + public readonly default_member_permissions: Permissions | null | undefined = undefined; + + public readonly dm_permission: boolean | undefined = undefined; + + public readonly nsfw: boolean | undefined = undefined; + /** * Sets whether the command is enabled by default when the application is added to a guild. * From d726ad2bdce9370bca509815a4da015a64eadeb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=86MB=C3=98?= Date: Fri, 10 May 2024 10:06:37 -0700 Subject: [PATCH 2/4] Add tests for types --- .../interactions/SlashCommands/SlashCommands.test.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts b/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts index 85e8878184d1..2f99165e2d2d 100644 --- a/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts +++ b/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts @@ -1,5 +1,5 @@ import { ChannelType, PermissionFlagsBits, type APIApplicationCommandOptionChoice } from 'discord-api-types/v10'; -import { describe, test, expect } from 'vitest'; +import { describe, test, expect, expectTypeOf } from 'vitest'; import { SlashCommandAssertions, SlashCommandBooleanOption, @@ -333,6 +333,16 @@ describe('Slash Commands', () => { // @ts-expect-error: Checking if not providing anything, or an invalid return type causes an error expect(() => getBuilder().addBooleanOption(() => new Collection())).toThrowError(); }); + + test("GIVEN valid builder with consistent method return types to builder, THEN does not throw error", () => { + type BuilderPropsOnly = Pick any ? never : Key]: any; + }>; + + expectTypeOf(getBuilder().addStringOption(getStringOption())).toMatchTypeOf(); + + expectTypeOf(getBuilder().addSubcommand(getSubcommand())).toMatchTypeOf(); + }); test('GIVEN valid builder with defaultPermission false THEN does not throw error', () => { expect(() => getBuilder().setName('foo').setDescription('foo').setDefaultPermission(false)).not.toThrowError(); From 58b761ad12ea7fcff16d43efaee12e7285fa49b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=86MB=C3=98?= Date: Fri, 10 May 2024 10:25:58 -0700 Subject: [PATCH 3/4] Fix formatting --- .../SlashCommands/SlashCommands.test.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts b/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts index 2f99165e2d2d..3999643d99fd 100644 --- a/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts +++ b/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts @@ -333,15 +333,18 @@ describe('Slash Commands', () => { // @ts-expect-error: Checking if not providing anything, or an invalid return type causes an error expect(() => getBuilder().addBooleanOption(() => new Collection())).toThrowError(); }); - - test("GIVEN valid builder with consistent method return types to builder, THEN does not throw error", () => { - type BuilderPropsOnly = Pick any ? never : Key]: any; - }>; - - expectTypeOf(getBuilder().addStringOption(getStringOption())).toMatchTypeOf(); - - expectTypeOf(getBuilder().addSubcommand(getSubcommand())).toMatchTypeOf(); + + test('GIVEN valid builder with consistent method return types to builder, THEN does not throw error', () => { + type BuilderPropsOnly = Pick< + Type, + keyof { + [Key in keyof Type as Type[Key] extends (...args: any) => any ? never : Key]: any; + } + >; + + expectTypeOf(getBuilder().addStringOption(getStringOption())).toMatchTypeOf(); + + expectTypeOf(getBuilder().addSubcommand(getSubcommand())).toMatchTypeOf(); }); test('GIVEN valid builder with defaultPermission false THEN does not throw error', () => { From 2135c0ac9dcd87e6071ba200fe06a4c055298e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=86MB=C3=98?= Date: Fri, 10 May 2024 10:29:24 -0700 Subject: [PATCH 4/4] Enable Vitest type checking --- .../SlashCommands/SlashCommands.test.ts | 15 +-------------- packages/builders/__tests__/types.test.ts | 17 +++++++++++++++++ vitest.config.ts | 4 ++++ 3 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 packages/builders/__tests__/types.test.ts diff --git a/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts b/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts index 3999643d99fd..85e8878184d1 100644 --- a/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts +++ b/packages/builders/__tests__/interactions/SlashCommands/SlashCommands.test.ts @@ -1,5 +1,5 @@ import { ChannelType, PermissionFlagsBits, type APIApplicationCommandOptionChoice } from 'discord-api-types/v10'; -import { describe, test, expect, expectTypeOf } from 'vitest'; +import { describe, test, expect } from 'vitest'; import { SlashCommandAssertions, SlashCommandBooleanOption, @@ -334,19 +334,6 @@ describe('Slash Commands', () => { expect(() => getBuilder().addBooleanOption(() => new Collection())).toThrowError(); }); - test('GIVEN valid builder with consistent method return types to builder, THEN does not throw error', () => { - type BuilderPropsOnly = Pick< - Type, - keyof { - [Key in keyof Type as Type[Key] extends (...args: any) => any ? never : Key]: any; - } - >; - - expectTypeOf(getBuilder().addStringOption(getStringOption())).toMatchTypeOf(); - - expectTypeOf(getBuilder().addSubcommand(getSubcommand())).toMatchTypeOf(); - }); - test('GIVEN valid builder with defaultPermission false THEN does not throw error', () => { expect(() => getBuilder().setName('foo').setDescription('foo').setDefaultPermission(false)).not.toThrowError(); }); diff --git a/packages/builders/__tests__/types.test.ts b/packages/builders/__tests__/types.test.ts new file mode 100644 index 000000000000..dfd6abef35f8 --- /dev/null +++ b/packages/builders/__tests__/types.test.ts @@ -0,0 +1,17 @@ +import { expectTypeOf } from 'vitest'; +import { SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder } from '../src/index.js'; + +const getBuilder = () => new SlashCommandBuilder(); +const getStringOption = () => new SlashCommandStringOption().setName('owo').setDescription('Testing 123'); +const getSubcommand = () => new SlashCommandSubcommandBuilder().setName('owo').setDescription('Testing 123'); + +type BuilderPropsOnly = Pick< + Type, + keyof { + [Key in keyof Type as Type[Key] extends (...args: any) => any ? never : Key]: any; + } +>; + +expectTypeOf(getBuilder().addStringOption(getStringOption())).toMatchTypeOf(); + +expectTypeOf(getBuilder().addSubcommand(getSubcommand())).toMatchTypeOf(); diff --git a/vitest.config.ts b/vitest.config.ts index c6e4f62c4e29..60e469dda8f4 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -4,6 +4,10 @@ export default defineConfig({ test: { exclude: ['**/node_modules', '**/dist', '.idea', '.git', '.cache'], passWithNoTests: true, + typecheck: { + enabled: true, + include: ["**/__tests__/types.test.ts"] + }, coverage: { enabled: true, all: true,