Would it be possible to make this have a generic type?

export function generateActionRows<T>(buttons: ButtonBuilder[]): ActionRowBuilder<T> {
const actionRows: ActionRowBuilder<ButtonBuilder>[] = [];
let currentRow = new ActionRowBuilder<ButtonBuilder>();

for (const [i, button] of buttons.entries()) {
currentRow.addComponents(button);
if (currentRow.components.length === 5 || i === buttons.length - 1) {
actionRows.push(currentRow);
currentRow = new ActionRowBuilder();

}
}

return actionRows;
}
export function generateActionRows<T>(buttons: ButtonBuilder[]): ActionRowBuilder<T> {
const actionRows: ActionRowBuilder<ButtonBuilder>[] = [];
let currentRow = new ActionRowBuilder<ButtonBuilder>();

for (const [i, button] of buttons.entries()) {
currentRow.addComponents(button);
if (currentRow.components.length === 5 || i === buttons.length - 1) {
actionRows.push(currentRow);
currentRow = new ActionRowBuilder();

}
}

return actionRows;
}
I've not really used them too much and have been trying to follow along using the Typescript documentation, however getting a bit confused when it comes to making it work whilst adhering to the limits of AnyComponentBuilder, as I do get an error with this current code:
Type 'T' does not satisfy the constraint 'AnyComponentBuilder'.
Type 'T' is not assignable to type 'TextInputBuilder'.ts(2344)
buttons.ts(23, 36): This type parameter might need an `extends TextInputBuilder` constraint.
buttons.ts(23, 36): This type parameter might need an `extends AnyComponentBuilder` constraint.
Type 'T' does not satisfy the constraint 'AnyComponentBuilder'.
Type 'T' is not assignable to type 'TextInputBuilder'.ts(2344)
buttons.ts(23, 36): This type parameter might need an `extends TextInputBuilder` constraint.
buttons.ts(23, 36): This type parameter might need an `extends AnyComponentBuilder` constraint.
I attempted to fix this by extending AnyCompontentBuilder and then I get the following error upon compilation within all of my usages of the generateActionRows
src/buttons/embed_save.ts:102:17 - error TS2322: Type 'ActionRowBuilder<AnyComponentBuilder>[]' is not assignable to type '(APIActionRowComponent<APIMessageActionRowComponent> | JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>> | ActionRowData<...>)[]'.

102 components: [..._rowGroup1, ..._rowGroup2, ..._rowGroup3, ..._rowGroup4]
src/buttons/embed_save.ts:102:17 - error TS2322: Type 'ActionRowBuilder<AnyComponentBuilder>[]' is not assignable to type '(APIActionRowComponent<APIMessageActionRowComponent> | JSONEncodable<APIActionRowComponent<APIMessageActionRowComponent>> | ActionRowData<...>)[]'.

102 components: [..._rowGroup1, ..._rowGroup2, ..._rowGroup3, ..._rowGroup4]
This is not much of an issue, just though tit would be nice if I could create one helper function for all ActionRowBuilder types rather than one per type.
3 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
oxi
oxiOP16mo ago
thanks 👍 not sure how i couldnt figure that out oh yeah i dont its just im also handlind rows in there i want to be explicit about it as textinputbuilder would only allow 1 per row, etc
Want results from more Discord servers?
Add your server