-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
Which package is this bug report for?
discord.js
Issue description
When iterating over fields in a ModalSubmitInteraction, the following TypeScript code can potentially throw an error:
// ctx is a ModalSubmitInteraction
for (const field of ctx.fields.components.filter((c) => c.type === ComponentType.Label)) {
const component = field.component;
if (component.type === ComponentType.FileUpload) {
const fieldAttachments = component.attachments.values().toArray() || [];
// do something
}
}The issue is that component.attachments can be undefined, but the typings do not reflect this possibility. As a result, accessing component.attachments.values() without an existence check can cause a runtime error. A workaround is to use optional chaining: component.attachments?.values().
Expected behavior: The typings should indicate that attachments may be undefined.
Actual behavior: No type error occurs, leading to a possible uncaught exception at runtime.
Suggested fix: Make the type for attachments possibly undefined.
Note: Yes, I could technically use .getUploadedFiles() but for the nature of my project I can't really do this...
Code sample
// ctx is a ModalSubmitInteraction
for (const field of ctx.fields.components.filter((c) => c.type === ComponentType.Label)) {
const comp = field.component;
if (comp.type === ComponentType.FileUpload) {
const fieldAttachments = comp.attachments.values().toArray() || [];
// do something
}
// workaround:
const fieldAttachments = comp.attachments?.values().toArray() || [];Versions
- discord.js:
14.25.1 - bun:
1.3.2 - typescript:
5.9.2 - OS:
- Development: macOS Tahoe 26.1
- Prod: Linux Ubuntu 24.04
Issue priority
Medium (should be fixed soon)
Which partials do you have configured?
ThreadMember
Which gateway intents are you subscribing to?
Guilds, GuildMembers, GuildModeration, GuildIntegrations, GuildWebhooks, GuildMessages, MessageContent
I have tested this issue on a development release
No response