Typescript: Missing type guard field for button components?

Code example:
import { ButtonComponentData, StringSelectMenuComponentData, UserSelectMenuComponentData } from "discord.js";

type Example1 = (
| ButtonComponentData
| StringSelectMenuComponentData
| UserSelectMenuComponentData
);

type Example2 = (
| StringSelectMenuComponentData
| UserSelectMenuComponentData
);

const example1: Example1 = {} as any;
const example2: Example2 = {} as any;

if (example1.type === ComponentType.StringSelect) {
// Type of example1: ButtonComponentData | StringSelectMenuComponentData
}

if (example2.type === ComponentType.StringSelect) {
// Type of example2: StringSelectMenuComponentData
}
import { ButtonComponentData, StringSelectMenuComponentData, UserSelectMenuComponentData } from "discord.js";

type Example1 = (
| ButtonComponentData
| StringSelectMenuComponentData
| UserSelectMenuComponentData
);

type Example2 = (
| StringSelectMenuComponentData
| UserSelectMenuComponentData
);

const example1: Example1 = {} as any;
const example2: Example2 = {} as any;

if (example1.type === ComponentType.StringSelect) {
// Type of example1: ButtonComponentData | StringSelectMenuComponentData
}

if (example2.type === ComponentType.StringSelect) {
// Type of example2: StringSelectMenuComponentData
}
I think the cause is that in BaseButtonComponentData, there lacks a field for typeguarding, i.e. type: ComponentType.Button while other components do.
1 Reply
d.js toolkit
d.js toolkit2y ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
Want results from more Discord servers?
Add your server