From 4bc1dae36f01649127774c40b14e778d65cf25c5 Mon Sep 17 00:00:00 2001 From: Aura Date: Sun, 12 Nov 2023 22:40:59 +0100 Subject: [PATCH] types: use wrapper utilities (#9945) * types: use `Awaitable` instead of `Promise | T` * types: use `JSONEncodable` over raw definition --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/typings/index.d.ts | 6 +++--- packages/discord.js/typings/index.test-d.ts | 3 ++- packages/rest/src/lib/utils/types.ts | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 7e4ed80ea..8614f2cea 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1129,7 +1129,7 @@ export abstract class Collector exten public toJSON(): unknown; protected listener: (...args: any[]) => void; - public abstract collect(...args: unknown[]): Key | null | Promise; + public abstract collect(...args: unknown[]): Awaitable; public abstract dispose(...args: unknown[]): Key | null; public on>( @@ -5066,7 +5066,7 @@ export interface CloseEvent { reason: string; } -export type CollectorFilter = (...args: Arguments) => boolean | Promise; +export type CollectorFilter = (...args: Arguments) => Awaitable; export interface CollectorOptions { filter?: CollectorFilter; @@ -6647,7 +6647,7 @@ export type Serialized = Value extends symbol | bigint | (() => any) ? never : Value extends number | string | boolean | undefined ? Value - : Value extends { toJSON(): infer JSONResult } + : Value extends JSONEncodable ? JSONResult : Value extends ReadonlyArray ? Serialized[] diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index f07e3e8b6..d81c9d524 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -185,6 +185,7 @@ import { PartialEmojiOnlyId, Emoji, PartialEmoji, + Awaitable, } from '.'; import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd'; import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders'; @@ -412,7 +413,7 @@ client.on('messageCreate', async message => { ( test: ButtonInteraction<'cached'>, items: Collection>, - ) => boolean | Promise + ) => Awaitable >(buttonCollector.filter); expectType(message.channel); expectType(message.guild); diff --git a/packages/rest/src/lib/utils/types.ts b/packages/rest/src/lib/utils/types.ts index 07070c850..fa9ea0337 100644 --- a/packages/rest/src/lib/utils/types.ts +++ b/packages/rest/src/lib/utils/types.ts @@ -1,6 +1,7 @@ import type { Readable } from 'node:stream'; import type { ReadableStream } from 'node:stream/web'; import type { Collection } from '@discordjs/collection'; +import type { Awaitable } from '@discordjs/util'; import type { Agent, Dispatcher, RequestInit, BodyInit, Response } from 'undici'; import type { IHandler } from '../interfaces/Handler.js'; @@ -183,7 +184,7 @@ export interface RateLimitData { /** * A function that determines whether the rate limit hit should throw an Error */ -export type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Promise | boolean; +export type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Awaitable; export interface APIRequest { /**