fix: button builder types

This commit is contained in:
Marcos Susaña 2024-03-12 00:15:05 -04:00
parent 10d66a221e
commit b8d511e3bf

View File

@ -1,7 +1,6 @@
import { throwError } from '..'; import { throwError } from '..';
import { import {
ComponentType, ComponentType,
type APIButtonComponent,
type APIButtonComponentWithCustomId, type APIButtonComponentWithCustomId,
type APIButtonComponentWithURL, type APIButtonComponentWithURL,
type APIMessageComponentEmoji, type APIMessageComponentEmoji,
@ -13,6 +12,14 @@ import { resolvePartialEmoji } from '../structures/extra/functions';
export type ButtonStylesForID = Exclude<ButtonStyle, ButtonStyle.Link>; export type ButtonStylesForID = Exclude<ButtonStyle, ButtonStyle.Link>;
export type ButtonSelect<T extends ButtonStyle> = T extends ButtonStyle.Link
? Omit<Button<false>, 'setCustomId' | 'setURL' | 'setStyle'> & {
setStyle(style: ButtonStyle.Link): Omit<Button<false>, 'setCustomId' | 'setURL' | 'setStyle'>;
}
: Omit<Button<true>, 'setCustomId' | 'setURL' | 'setStyle'> & {
setStyle(style: ButtonStylesForID): Omit<Button<true>, 'setCustomId' | 'setURL' | 'setStyle'>;
};
/** /**
* Represents a button component. * Represents a button component.
* @template Type - The type of the button component. * @template Type - The type of the button component.
@ -31,10 +38,10 @@ export class Button<Type extends boolean = boolean> {
* @param id - The custom ID to set. * @param id - The custom ID to set.
* @returns The modified Button instance. * @returns The modified Button instance.
*/ */
setCustomId(id: string) { setCustomId(id: string): ButtonSelect<ButtonStylesForID> {
// @ts-expect-error // @ts-expect-error
this.data.custom_id = id; this.data.custom_id = id;
return this; return this as ButtonSelect<ButtonStylesForID>;
} }
/** /**
@ -42,10 +49,10 @@ export class Button<Type extends boolean = boolean> {
* @param url - The URL to set. * @param url - The URL to set.
* @returns The modified Button instance. * @returns The modified Button instance.
*/ */
setURL(url: string) { setURL(url: string): ButtonSelect<ButtonStyle.Link> {
// @ts-expect-error // @ts-expect-error
this.data.url = url; this.data.url = url;
return this; return this as ButtonSelect<ButtonStyle.Link>;
} }
/** /**
@ -80,6 +87,8 @@ export class Button<Type extends boolean = boolean> {
return this; return this;
} }
setStyle(style: ButtonStyle.Link): Omit<Button, 'setCustomId'>;
setStyle(style: ButtonStylesForID): Omit<Button, 'setURL'>;
setStyle(style: ButtonStyle) { setStyle(style: ButtonStyle) {
this.data.style = style; this.data.style = style;
return this; return this;
@ -90,6 +99,6 @@ export class Button<Type extends boolean = boolean> {
* @returns The JSON representation of the Button instance. * @returns The JSON representation of the Button instance.
*/ */
toJSON() { toJSON() {
return { ...this.data } as unknown as APIButtonComponent; return { ...this.data } as When<Type, APIButtonComponentWithCustomId, APIButtonComponentWithURL>;
} }
} }