I decided to create a Discord bot using DiscordJS and TypeScript. To simplify the process of adding components to Discord messages, I developed an abstract class called componentprototype. Here is how it looks (Please note that Generators are subclasses like ButtonBuilder etc.)
import { Awaitable, ButtonInteraction, StringSelectMenuInteraction, BaseInteraction} from "discord.js"
import { Logger, MyClient, ComponentGenerators } from "./index.js"
import { ButtonGenerator, StringSelectGenerator } from "../utils/index.js";
export interface CallbackProps<Interaction extends BaseInteraction | null> {
interaction: Interaction,
client: MyClient,
log: Logger,
}
// more code...
export class Button extends ComponentPrototype<ButtonGenerator> {
constructor(data: ComponentProtoData<ButtonGenerator>) {
super(data)
}
}
Now, I wanted to create a Type that can contain an Array of all possible Components using:
const components: ComponentPrototype<ComponentGenerators>[] = [new Button({id: "test", generator: ButtonGenerator.Back()})]
Error:
Type 'Button' cannot be assigned to type 'ComponentPrototype<ComponentGenerators>'.
Types of property 'callback' are incompatible.
Type 'Callback<unknown, ButtonInteraction<CacheType>, unknown[]> | undefined' cannot be assigned to type 'Callback<unknown, ButtonInteraction<CacheType> | StringSelectMenuInteraction<CacheType>, unknown[]> | undefined'.
Type 'Callback<unknown, ButtonInteraction<CacheType>, unknown[]>' cannot be assigned to type 'Callback<unknown, ButtonInteraction<CacheType> | StringSelectMenuInteraction<CacheType>, unknown[]>'.
Types of parameters 'props' and 'props' are not compatible.
Type 'CallbackProps<ButtonInteraction<CacheType> | StringSelectMenuInteraction<CacheType>>' cannot be assigned to type 'CallbackProps<ButtonInteraction<CacheType>>'.
Type 'ButtonInteraction<CacheType> | StringSelectMenuInteraction<CacheType>' cannot be assigned to type 'ButtonInteraction<CacheType>'.
Type 'StringSelectMenuInteraction<CacheType>' cannot be assigned to type 'ButtonInteraction<CacheType>'.ts(2322)