Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(Partials): add toString() method to supported Partials #9835

Merged
merged 11 commits into from
Oct 10, 2023
30 changes: 19 additions & 11 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6107,22 +6107,28 @@ 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];
};
janparisek marked this conversation as resolved.
Show resolved Hide resolved

export interface PartialDMChannel extends Partialize<DMChannel, null, null, 'lastMessageId'> {
export interface PartialDMChannel extends Partialize<DMChannel, null, null, 'lastMessageId' | 'toString'> {
lastMessageId: undefined;
toString(): ReturnType<DMChannel['toString']>;
}

export interface PartialGuildMember extends Partialize<GuildMember, 'joinedAt' | 'joinedTimestamp' | 'pending'> {}
export interface PartialGuildMember
extends Partialize<GuildMember, 'joinedAt' | 'joinedTimestamp' | 'pending', null, 'toString'> {
toString(): ReturnType<GuildMember['toString']>;
}

export interface PartialMessage
extends Partialize<Message, 'type' | 'system' | 'pinned' | 'tts', 'content' | 'cleanContent' | 'author'> {}
Expand Down Expand Up @@ -6152,7 +6158,9 @@ export enum Partials {
ThreadMember,
}

export interface PartialUser extends Partialize<User, 'username' | 'tag' | 'discriminator'> {}
export interface PartialUser extends Partialize<User, 'username' | 'tag' | 'discriminator', null, 'toString'> {
toString(): ReturnType<User['toString']>;
}

export type PresenceStatusData = ClientPresenceStatus | 'invisible';

Expand Down
37 changes: 37 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ import {
GuildOnboarding,
StringSelectMenuComponentData,
ButtonComponentData,
PartialDMChannel,
PartialGuildMember,
PartialMessage,
PartialMessageReaction,
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -2315,3 +2319,36 @@ expectType<Readonly<GuildMemberFlagsBitField>>(guildMember.flags);
const onboarding = await guild.fetchOnboarding();
expectType<GuildOnboarding>(onboarding);
}

declare const partialDMChannel: PartialDMChannel;
expectType<undefined>(partialDMChannel.lastMessageId);
expectType<ReturnType<DMChannel['toString']>>(partialDMChannel.toString());

declare const partialGuildMember: PartialGuildMember;
expectType<null>(partialGuildMember.joinedAt);
expectType<null>(partialGuildMember.joinedTimestamp);
expectType<null>(partialGuildMember.pending);
expectType<ReturnType<GuildMember['toString']>>(partialGuildMember.toString());

declare const partialMessage: PartialMessage;
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<null>(partialMessageReaction.count);

declare const partialThreadMeber: PartialThreadMember;
expectType<null>(partialThreadMeber.flags);
expectType<null>(partialThreadMeber.joinedAt);
expectType<null>(partialThreadMeber.joinedTimestamp);

declare const partialUser: PartialUser;
expectType<null>(partialUser.username);
expectType<null>(partialUser.tag);
expectType<null>(partialUser.discriminator);
expectType<ReturnType<User['toString']>>(partialUser.toString());