chore: use descriptive type parameter names (#9937)

* chore: use descriptive type parameter names

* refactor: requested changes

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Almeida
2023-11-12 17:21:51 +00:00
committed by GitHub
parent 4ff3ea4a1b
commit 975d5f18ae
34 changed files with 1104 additions and 895 deletions

View File

@@ -54,15 +54,15 @@ export type AnyComponentBuilder = MessageActionRowComponentBuilder | ModalAction
/**
* A builder that creates API-compatible JSON data for action rows.
*
* @typeParam T - The types of components this action row holds
* @typeParam ComponentType - The types of components this action row holds
*/
export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBuilder<
export class ActionRowBuilder<ComponentType extends AnyComponentBuilder> extends ComponentBuilder<
APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>
> {
/**
* The components within this action row.
*/
public readonly components: T[];
public readonly components: ComponentType[];
/**
* Creates a new action row from API data.
@@ -100,7 +100,7 @@ export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBu
*/
public constructor({ components, ...data }: Partial<APIActionRowComponent<APIActionRowComponentTypes>> = {}) {
super({ type: ComponentType.ActionRow, ...data });
this.components = (components?.map((component) => createComponentBuilder(component)) ?? []) as T[];
this.components = (components?.map((component) => createComponentBuilder(component)) ?? []) as ComponentType[];
}
/**
@@ -108,7 +108,7 @@ export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBu
*
* @param components - The components to add
*/
public addComponents(...components: RestOrArray<T>) {
public addComponents(...components: RestOrArray<ComponentType>) {
this.components.push(...normalizeArray(components));
return this;
}
@@ -118,7 +118,7 @@ export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBu
*
* @param components - The components to set
*/
public setComponents(...components: RestOrArray<T>) {
public setComponents(...components: RestOrArray<ComponentType>) {
this.components.splice(0, this.components.length, ...normalizeArray(components));
return this;
}
@@ -126,10 +126,10 @@ export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBu
/**
* {@inheritDoc ComponentBuilder.toJSON}
*/
public toJSON(): APIActionRowComponent<ReturnType<T['toJSON']>> {
public toJSON(): APIActionRowComponent<ReturnType<ComponentType['toJSON']>> {
return {
...this.data,
components: this.components.map((component) => component.toJSON()),
} as APIActionRowComponent<ReturnType<T['toJSON']>>;
} as APIActionRowComponent<ReturnType<ComponentType['toJSON']>>;
}
}

View File

@@ -55,21 +55,23 @@ export interface MappedComponentTypes {
/**
* Factory for creating components from API data.
*
* @typeParam T - The type of component to use
* @typeParam ComponentType - The type of component to use
* @param data - The API data to transform to a component class
*/
export function createComponentBuilder<T extends keyof MappedComponentTypes>(
export function createComponentBuilder<ComponentType extends keyof MappedComponentTypes>(
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
data: (APIModalComponent | APIMessageComponent) & { type: T },
): MappedComponentTypes[T];
data: (APIModalComponent | APIMessageComponent) & { type: ComponentType },
): MappedComponentTypes[ComponentType];
/**
* Factory for creating components from API data.
*
* @typeParam C - The type of component to use
* @typeParam ComponentBuilder - The type of component to use
* @param data - The API data to transform to a component class
*/
export function createComponentBuilder<C extends MessageComponentBuilder | ModalComponentBuilder>(data: C): C;
export function createComponentBuilder<ComponentBuilder extends MessageComponentBuilder | ModalComponentBuilder>(
data: ComponentBuilder,
): ComponentBuilder;
export function createComponentBuilder(
data: APIMessageComponent | APIModalComponent | MessageComponentBuilder,

View File

@@ -66,8 +66,8 @@ export function validateChoicesLength(amountAdding: number, choices?: APIApplica
}
export function assertReturnOfBuilder<
T extends ApplicationCommandOptionBase | SlashCommandSubcommandBuilder | SlashCommandSubcommandGroupBuilder,
>(input: unknown, ExpectedInstanceOf: new () => T): asserts input is T {
ReturnType extends ApplicationCommandOptionBase | SlashCommandSubcommandBuilder | SlashCommandSubcommandGroupBuilder,
>(input: unknown, ExpectedInstanceOf: new () => ReturnType): asserts input is ReturnType {
s.instance(ExpectedInstanceOf).parse(input);
}

View File

@@ -14,11 +14,11 @@ const booleanPredicate = s.boolean;
/**
* This mixin holds choices and autocomplete symbols used for options.
*/
export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends number | string> {
export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<ChoiceType extends number | string> {
/**
* The choices of this option.
*/
public readonly choices?: APIApplicationCommandOptionChoice<T>[];
public readonly choices?: APIApplicationCommandOptionChoice<ChoiceType>[];
/**
* Whether this option utilizes autocomplete.
@@ -37,7 +37,7 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends n
*
* @param choices - The choices to add
*/
public addChoices(...choices: APIApplicationCommandOptionChoice<T>[]): this {
public addChoices(...choices: APIApplicationCommandOptionChoice<ChoiceType>[]): this {
if (choices.length > 0 && this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
@@ -69,7 +69,7 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends n
*
* @param choices - The choices to set
*/
public setChoices<Input extends APIApplicationCommandOptionChoice<T>[]>(...choices: Input): this {
public setChoices<Input extends APIApplicationCommandOptionChoice<ChoiceType>[]>(...choices: Input): this {
if (choices.length > 0 && this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}

View File

@@ -148,13 +148,15 @@ export class SharedSlashCommandOptions<ShouldOmitSubcommandFunctions = true> {
* @param Instance - The instance of whatever is being added
* @internal
*/
private _sharedAddOptionMethod<T extends ApplicationCommandOptionBase>(
private _sharedAddOptionMethod<OptionBuilder extends ApplicationCommandOptionBase>(
input:
| Omit<T, 'addChoices'>
| Omit<T, 'setAutocomplete'>
| T
| ((builder: T) => Omit<T, 'addChoices'> | Omit<T, 'setAutocomplete'> | T),
Instance: new () => T,
| Omit<OptionBuilder, 'addChoices'>
| Omit<OptionBuilder, 'setAutocomplete'>
| OptionBuilder
| ((
builder: OptionBuilder,
) => Omit<OptionBuilder, 'addChoices'> | Omit<OptionBuilder, 'setAutocomplete'> | OptionBuilder),
Instance: new () => OptionBuilder,
): ShouldOmitSubcommandFunctions extends true ? Omit<this, 'addSubcommand' | 'addSubcommandGroup'> : this {
const { options } = this;

View File

@@ -1,12 +1,12 @@
/**
* Normalizes data that is a rest parameter or an array into an array with a depth of 1.
*
* @typeParam T - The data that must satisfy {@link RestOrArray}.
* @typeParam ItemType - The data that must satisfy {@link RestOrArray}.
* @param arr - The (possibly variadic) data to normalize
*/
export function normalizeArray<T>(arr: RestOrArray<T>): T[] {
export function normalizeArray<ItemType>(arr: RestOrArray<ItemType>): ItemType[] {
if (Array.isArray(arr[0])) return arr[0];
return arr as T[];
return arr as ItemType[];
}
/**
@@ -16,4 +16,4 @@ export function normalizeArray<T>(arr: RestOrArray<T>): T[] {
* This type is used throughout builders to ensure both an array and variadic arguments
* may be used. It is normalized with {@link normalizeArray}.
*/
export type RestOrArray<T> = T[] | [T[]];
export type RestOrArray<Type> = Type[] | [Type[]];