Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
types(Partials): add toString() method to supported Partials (#9835)
* types(Partials): add toString() method to supported Partials

- includes `PartialDMChannel`, `PartialGuildMember` and `PartialUser`
- does not include
    - `PartialMessage`, since `<PartialMessage>.content` is always null
    - `PartialMessageReaction`, since `MessageReaction` has no `toString()` method
    - `PartialThreadMember`, since `ThreadMember` has no `toString()` method

* types(Partials): replace type of `toString()` methods with return type

* test(Partials): add tests

* types(Partialize): refactor Partialize

* test(PartialThreadMember): fix typo

* types(Partials): clean up unnecessary type overrides

* test(Partials): add tests for <Partials>.partial property

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
  • Loading branch information
janparisek and Jiralite committed Oct 10, 2023
1 parent 1fe7247 commit 7422d9f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -6161,15 +6161,17 @@ export interface PartialChannelData {

export type Partialize<
T extends AllowedPartial,
N extends keyof T | null = null,
M extends keyof T | null = null,
E extends keyof T | '' = '',
NulledKeys extends keyof T | null = null,
NullableKeys extends keyof T | null = null,
OverridableKeys extends keyof T | '' = '',
> = {
readonly client: Client<true>;
id: Snowflake;
partial: true;
} & {
[K in keyof Omit<T, 'client' | 'id' | 'partial' | E>]: K extends N ? null : K extends M ? T[K] | null : T[K];
[K in keyof Omit<T, OverridableKeys>]: K extends 'partial'
? true
: K extends NulledKeys
? null
: K extends NullableKeys
? T[K] | null
: T[K];
};

export interface PartialDMChannel extends Partialize<DMChannel, null, null, 'lastMessageId'> {
Expand Down
40 changes: 40 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Expand Up @@ -177,6 +177,10 @@ import {
StringSelectMenuComponentData,
ButtonComponentData,
MediaChannel,
PartialDMChannel,
PartialGuildMember,
PartialMessage,
PartialMessageReaction,
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -2323,3 +2327,39 @@ expectType<Readonly<GuildMemberFlagsBitField>>(guildMember.flags);
const onboarding = await guild.fetchOnboarding();
expectType<GuildOnboarding>(onboarding);
}

declare const partialDMChannel: PartialDMChannel;
expectType<true>(partialDMChannel.partial);
expectType<undefined>(partialDMChannel.lastMessageId);

declare const partialGuildMember: PartialGuildMember;
expectType<true>(partialGuildMember.partial);
expectType<null>(partialGuildMember.joinedAt);
expectType<null>(partialGuildMember.joinedTimestamp);
expectType<null>(partialGuildMember.pending);

declare const partialMessage: PartialMessage;
expectType<true>(partialMessage.partial);
expectType<null>(partialMessage.type);
expectType<null>(partialMessage.system);
expectType<null>(partialMessage.pinned);
expectType<null>(partialMessage.tts);
expectAssignable<null | Message['content']>(partialMessage.content);
expectAssignable<null | Message['cleanContent']>(partialMessage.cleanContent);
expectAssignable<null | Message['author']>(partialMessage.author);

declare const partialMessageReaction: PartialMessageReaction;
expectType<true>(partialMessageReaction.partial);
expectType<null>(partialMessageReaction.count);

declare const partialThreadMember: PartialThreadMember;
expectType<true>(partialThreadMember.partial);
expectType<null>(partialThreadMember.flags);
expectType<null>(partialThreadMember.joinedAt);
expectType<null>(partialThreadMember.joinedTimestamp);

declare const partialUser: PartialUser;
expectType<true>(partialUser.partial);
expectType<null>(partialUser.username);
expectType<null>(partialUser.tag);
expectType<null>(partialUser.discriminator);

0 comments on commit 7422d9f

Please sign in to comment.