Skip to content

Commit

Permalink
fix(APIChannel): correctly type name based on channel type (#666)
Browse files Browse the repository at this point in the history
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
  • Loading branch information
cyyynthia and vladfrangu committed Dec 11, 2022
1 parent 549d1da commit 995126e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 40 deletions.
29 changes: 19 additions & 10 deletions deno/payloads/v10/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export interface APIPartialChannel {
*/
type: ChannelType;
/**
* The name of the channel (2-100 characters)
* The name of the channel (1-100 characters)
*/
name?: string;
name?: string | null;
}

/**
Expand Down Expand Up @@ -69,7 +69,11 @@ export interface APITextBasedChannel<T extends ChannelType> extends APIChannelBa
rate_limit_per_user?: number;
}

export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T> {
export interface APIGuildChannel<T extends ChannelType> extends Omit<APIChannelBase<T>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string;
/**
* The id of the guild (may be missing for some channel objects received over gateway guild dispatches)
*/
Expand Down Expand Up @@ -101,7 +105,7 @@ export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T
export type GuildTextChannelType = Exclude<TextChannelType, ChannelType.DM | ChannelType.GroupDM>;

export interface APIGuildTextChannel<T extends GuildTextChannelType>
extends APITextBasedChannel<T>,
extends Omit<APITextBasedChannel<T>, 'name'>,
APIGuildChannel<T> {
/**
* Default duration for newly created threads, in minutes, to automatically archive the thread after recent activity
Expand Down Expand Up @@ -141,7 +145,7 @@ export interface APIVoiceChannelBase<T extends ChannelType> extends APIGuildChan

export interface APIGuildVoiceChannel
extends APIVoiceChannelBase<ChannelType.GuildVoice>,
APITextBasedChannel<ChannelType.GuildVoice> {
Omit<APITextBasedChannel<ChannelType.GuildVoice>, 'name'> {
/**
* The camera video quality mode of the voice channel, `1` when not present
*
Expand All @@ -161,9 +165,18 @@ export interface APIDMChannelBase<T extends ChannelType> extends Omit<APITextBas
recipients?: APIUser[];
}

export type APIDMChannel = APIDMChannelBase<ChannelType.DM>;
export interface APIDMChannel extends Omit<APIDMChannelBase<ChannelType.DM>, 'name'> {
/**
* The name of the channel (always null for DM channels)
*/
name: null;
}

export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.GroupDM>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string | null;
/**
* Application id of the group DM creator if it is bot-created
*/
Expand All @@ -172,10 +185,6 @@ export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.Gro
* Icon hash
*/
icon?: string | null;
/**
* The name of the channel (2-100 characters)
*/
name?: string | null;
/**
* ID of the DM creator
*/
Expand Down
29 changes: 19 additions & 10 deletions deno/payloads/v9/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export interface APIPartialChannel {
*/
type: ChannelType;
/**
* The name of the channel (2-100 characters)
* The name of the channel (1-100 characters)
*/
name?: string;
name?: string | null;
}

/**
Expand Down Expand Up @@ -69,7 +69,11 @@ export interface APITextBasedChannel<T extends ChannelType> extends APIChannelBa
rate_limit_per_user?: number;
}

export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T> {
export interface APIGuildChannel<T extends ChannelType> extends Omit<APIChannelBase<T>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string;
/**
* The id of the guild (may be missing for some channel objects received over gateway guild dispatches)
*/
Expand Down Expand Up @@ -101,7 +105,7 @@ export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T
export type GuildTextChannelType = Exclude<TextChannelType, ChannelType.DM | ChannelType.GroupDM>;

export interface APIGuildTextChannel<T extends GuildTextChannelType>
extends APITextBasedChannel<T>,
extends Omit<APITextBasedChannel<T>, 'name'>,
APIGuildChannel<T> {
/**
* Default duration for newly created threads, in minutes, to automatically archive the thread after recent activity
Expand Down Expand Up @@ -141,7 +145,7 @@ export interface APIVoiceChannelBase<T extends ChannelType> extends APIGuildChan

export interface APIGuildVoiceChannel
extends APIVoiceChannelBase<ChannelType.GuildVoice>,
APITextBasedChannel<ChannelType.GuildVoice> {
Omit<APITextBasedChannel<ChannelType.GuildVoice>, 'name'> {
/**
* The camera video quality mode of the voice channel, `1` when not present
*
Expand All @@ -161,9 +165,18 @@ export interface APIDMChannelBase<T extends ChannelType> extends Omit<APITextBas
recipients?: APIUser[];
}

export type APIDMChannel = APIDMChannelBase<ChannelType.DM>;
export interface APIDMChannel extends Omit<APIDMChannelBase<ChannelType.DM>, 'name'> {
/**
* The name of the channel (always null for DM channels)
*/
name: null;
}

export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.GroupDM>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string | null;
/**
* Application id of the group DM creator if it is bot-created
*/
Expand All @@ -172,10 +185,6 @@ export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.Gro
* Icon hash
*/
icon?: string | null;
/**
* The name of the channel (2-100 characters)
*/
name?: string | null;
/**
* ID of the DM creator
*/
Expand Down
29 changes: 19 additions & 10 deletions payloads/v10/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export interface APIPartialChannel {
*/
type: ChannelType;
/**
* The name of the channel (2-100 characters)
* The name of the channel (1-100 characters)
*/
name?: string;
name?: string | null;
}

/**
Expand Down Expand Up @@ -69,7 +69,11 @@ export interface APITextBasedChannel<T extends ChannelType> extends APIChannelBa
rate_limit_per_user?: number;
}

export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T> {
export interface APIGuildChannel<T extends ChannelType> extends Omit<APIChannelBase<T>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string;
/**
* The id of the guild (may be missing for some channel objects received over gateway guild dispatches)
*/
Expand Down Expand Up @@ -101,7 +105,7 @@ export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T
export type GuildTextChannelType = Exclude<TextChannelType, ChannelType.DM | ChannelType.GroupDM>;

export interface APIGuildTextChannel<T extends GuildTextChannelType>
extends APITextBasedChannel<T>,
extends Omit<APITextBasedChannel<T>, 'name'>,
APIGuildChannel<T> {
/**
* Default duration for newly created threads, in minutes, to automatically archive the thread after recent activity
Expand Down Expand Up @@ -141,7 +145,7 @@ export interface APIVoiceChannelBase<T extends ChannelType> extends APIGuildChan

export interface APIGuildVoiceChannel
extends APIVoiceChannelBase<ChannelType.GuildVoice>,
APITextBasedChannel<ChannelType.GuildVoice> {
Omit<APITextBasedChannel<ChannelType.GuildVoice>, 'name'> {
/**
* The camera video quality mode of the voice channel, `1` when not present
*
Expand All @@ -161,9 +165,18 @@ export interface APIDMChannelBase<T extends ChannelType> extends Omit<APITextBas
recipients?: APIUser[];
}

export type APIDMChannel = APIDMChannelBase<ChannelType.DM>;
export interface APIDMChannel extends Omit<APIDMChannelBase<ChannelType.DM>, 'name'> {
/**
* The name of the channel (always null for DM channels)
*/
name: null;
}

export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.GroupDM>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string | null;
/**
* Application id of the group DM creator if it is bot-created
*/
Expand All @@ -172,10 +185,6 @@ export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.Gro
* Icon hash
*/
icon?: string | null;
/**
* The name of the channel (2-100 characters)
*/
name?: string | null;
/**
* ID of the DM creator
*/
Expand Down
29 changes: 19 additions & 10 deletions payloads/v9/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export interface APIPartialChannel {
*/
type: ChannelType;
/**
* The name of the channel (2-100 characters)
* The name of the channel (1-100 characters)
*/
name?: string;
name?: string | null;
}

/**
Expand Down Expand Up @@ -69,7 +69,11 @@ export interface APITextBasedChannel<T extends ChannelType> extends APIChannelBa
rate_limit_per_user?: number;
}

export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T> {
export interface APIGuildChannel<T extends ChannelType> extends Omit<APIChannelBase<T>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string;
/**
* The id of the guild (may be missing for some channel objects received over gateway guild dispatches)
*/
Expand Down Expand Up @@ -101,7 +105,7 @@ export interface APIGuildChannel<T extends ChannelType> extends APIChannelBase<T
export type GuildTextChannelType = Exclude<TextChannelType, ChannelType.DM | ChannelType.GroupDM>;

export interface APIGuildTextChannel<T extends GuildTextChannelType>
extends APITextBasedChannel<T>,
extends Omit<APITextBasedChannel<T>, 'name'>,
APIGuildChannel<T> {
/**
* Default duration for newly created threads, in minutes, to automatically archive the thread after recent activity
Expand Down Expand Up @@ -141,7 +145,7 @@ export interface APIVoiceChannelBase<T extends ChannelType> extends APIGuildChan

export interface APIGuildVoiceChannel
extends APIVoiceChannelBase<ChannelType.GuildVoice>,
APITextBasedChannel<ChannelType.GuildVoice> {
Omit<APITextBasedChannel<ChannelType.GuildVoice>, 'name'> {
/**
* The camera video quality mode of the voice channel, `1` when not present
*
Expand All @@ -161,9 +165,18 @@ export interface APIDMChannelBase<T extends ChannelType> extends Omit<APITextBas
recipients?: APIUser[];
}

export type APIDMChannel = APIDMChannelBase<ChannelType.DM>;
export interface APIDMChannel extends Omit<APIDMChannelBase<ChannelType.DM>, 'name'> {
/**
* The name of the channel (always null for DM channels)
*/
name: null;
}

export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.GroupDM>, 'name'> {
/**
* The name of the channel (1-100 characters)
*/
name: string | null;
/**
* Application id of the group DM creator if it is bot-created
*/
Expand All @@ -172,10 +185,6 @@ export interface APIGroupDMChannel extends Omit<APIDMChannelBase<ChannelType.Gro
* Icon hash
*/
icon?: string | null;
/**
* The name of the channel (2-100 characters)
*/
name?: string | null;
/**
* ID of the DM creator
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/v10/channel.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expectType } from 'tsd';
import type { ChannelType, APIPartialChannel, APIGroupDMChannel, APIDMChannel, APIGuildChannel } from '../../v10';

declare const partialChannel: APIPartialChannel;
declare const groupDMChannel: APIGroupDMChannel;
declare const dmChannel: APIDMChannel;
declare const guildChannel: APIGuildChannel<ChannelType>;

// Test channel names are properly typed
// Always non-null present for non-DM channels, always null for DM channel
expectType<string | null | undefined>(partialChannel.name);
expectType<string | null>(groupDMChannel.name);
expectType<null>(dmChannel.name);
expectType<string>(guildChannel.name);

1 comment on commit 995126e

@vercel
Copy link

@vercel vercel bot commented on 995126e Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.