From 45f7e1a2e85da760f548765b768bd1b378bdedb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= <9092381+Renegade334@users.noreply.github.com> Date: Tue, 20 Aug 2024 10:20:35 +0100 Subject: [PATCH] fix(GuildAuditLogsEntry): correct mapped `AuditLogChange` objects (#10438) * refactor(GuildAuditLogsEntry): correct mapped AuditLogChange objects * test: check union narrowing behaviour of AuditLogChange --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../discord.js/src/structures/GuildAuditLogsEntry.js | 6 +++++- packages/discord.js/typings/index.d.ts | 12 +++++++----- packages/discord.js/typings/index.test-d.ts | 11 +++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/discord.js/src/structures/GuildAuditLogsEntry.js b/packages/discord.js/src/structures/GuildAuditLogsEntry.js index 1fec90c740b8..1a1cadd33e14 100644 --- a/packages/discord.js/src/structures/GuildAuditLogsEntry.js +++ b/packages/discord.js/src/structures/GuildAuditLogsEntry.js @@ -172,7 +172,11 @@ class GuildAuditLogsEntry { * @type {AuditLogChange[]} */ this.changes = - data.changes?.map(change => ({ key: change.key, old: change.old_value, new: change.new_value })) ?? []; + data.changes?.map(change => ({ + key: change.key, + ...('old_value' in change ? { old: change.old_value } : {}), + ...('new_value' in change ? { new: change.new_value } : {}), + })) ?? []; /** * The entry's id diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 4b36c342ab2d..8a998c206526 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4991,11 +4991,13 @@ export interface ApplicationRoleConnectionMetadataEditOptions { type: ApplicationRoleConnectionMetadataType; } -export interface AuditLogChange { - key: APIAuditLogChange['key']; - old?: APIAuditLogChange['old_value']; - new?: APIAuditLogChange['new_value']; -} +export type AuditLogChange = { + [SourceElement in APIAuditLogChange as SourceElement['key']]: { + key: SourceElement['key']; + old?: SourceElement['old_value']; + new?: SourceElement['new_value']; + }; +}[APIAuditLogChange['key']]; export interface AutoModerationAction { type: AutoModerationActionType; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index 407f6023e8d2..833252c9231d 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -104,6 +104,7 @@ import { Collector, GuildAuditLogsEntry, GuildAuditLogs, + type AuditLogChange, StageInstance, ActionRowBuilder, ButtonComponent, @@ -2171,6 +2172,16 @@ expectType>( guild.fetchAuditLogs({ type: AuditLogEvent.MessageDelete }).then(al => al.entries.first()?.target), ); +declare const AuditLogChange: AuditLogChange; +// @ts-expect-error +expectType(AuditLogChange.old); +// @ts-expect-error +expectType(AuditLogChange.new); +if (AuditLogChange.key === 'available') { + expectType(AuditLogChange.old); + expectType(AuditLogChange.new); +} + declare const TextBasedChannel: TextBasedChannel; declare const TextBasedChannelTypes: TextBasedChannelTypes; declare const VoiceBasedChannel: VoiceBasedChannel;