Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Typings): type attachments to InteractionUpdateOptions #6162

Merged
merged 4 commits into from Jul 23, 2021
Merged

fix(Typings): type attachments to InteractionUpdateOptions #6162

merged 4 commits into from Jul 23, 2021

Conversation

Milo123459
Copy link
Contributor

Please describe the changes this PR makes and why it should be merged:

Currently, if you want to remove attachments from a message you have to use message.removeAttachments(), and if you need to edit the content as well that is 2 API calls. Assigning attachments: [] does work, but sparks errors in TypeScript that require // @ts-ignore - which is generally considered a bad practice. This PR fixes the typings allowing you to assign attachments: [] without having to use ts-ignore.

Status and versioning classification:

I know how to update typings and have done so, or typings don't need updating

@DTrombett
Copy link
Contributor

const attachments: MessageAttachment[] = [];
msg.edit({ attachments, content: 'abc' });

This works without using TS ignore...

@Milo123459
Copy link
Contributor Author

If that works then logically speaking I would expect attachments: [] as MessageAttachment[] to work but I still get the same error if I just gave it a blank array

typings/index.d.ts Outdated Show resolved Hide resolved
Co-authored-by: DTrombett <73136330+DTrombett@users.noreply.github.com>
@Milo123459
Copy link
Contributor Author

Also, this error also pops up when using <MessageComponentInteraction>.update({attachments: []}) - I can't seem to find an interface that corrosponds to the attachments field. It's all just chains of Omit's

@monbrey
Copy link
Member

monbrey commented Jul 21, 2021

I can't replicate any issue with message.edit({ attachments: [] }), TypeScript isnt complaining.

@Milo123459
Copy link
Contributor Author

Odd. I also get this error when trying to update a MesaageComponentInteraction with no attachments.

@monbrey
Copy link
Member

monbrey commented Jul 21, 2021

attachments is not a valid property for InteractionUpdateOptions. We briefly discussed this in dev chats but never applied a fix. Ignoring the Omit typings, it's basically a chain.

InteractionUpdateOptions > InteractionReplyOptions > WebhookMessageOptions > MessageOptions

This isn't quite what it should be; InteractionUpdateOptions should probably be something more like:

export interface InteractionUpdateOptions extends Omit<WebhookEditMessageOptions, 'username' | 'avatarURL'> {
  fetchReply?: boolean;
}

and WebhookEditMessageOptions probably needs to support editing attachments, so:

export interface WebhookEditMessageOptions extends Pick<
  WebhookMessageOptions,
  'content' | 'embeds' | 'files' | 'allowedMentions' | 'components'
> {
  attachments: MessageAttachment[];
}

Or just scrap picking from WebookMessageOptions and pick from MessageEditOptions instead?

@Milo123459
Copy link
Contributor Author

Picking from MessageOptions makes more sense. I'll try to implement a fix.

@Milo123459
Copy link
Contributor Author

export interface WebhookEditMessageOptions extends Pick<
  WebhookMessageOptions,
  'content' | 'embeds' | 'files' | 'allowedMentions' | 'components'
> {
  attachments: MessageAttachment[];
}

From what you said, this implies that when you are assigning attachments it'll cause the same error this is PR trying to fix. I'll go with attachments: (MessageAttachment | never)[] as that's what this PR is fixing.

@monbrey
Copy link
Member

monbrey commented Jul 21, 2021

You're referring to an error I was unable to replicate on message.edit. I don't believe typing it as never is the correct fix for the issue you described, which does exist on interaction.update as no typings for attachments exist as all.

For comparison, you can also remove components and embeds by setting those to empty arrays, and those don't have never typings. It's not the right fix.

@Milo123459
Copy link
Contributor Author

So all this has to do is type attachments for InteractionUpdateOptions?

@monbrey
Copy link
Member

monbrey commented Jul 21, 2021

So all this has to do is type attachments for InteractionUpdateOptions?

I think so, yes. But as its a long chain of Omits as you pointed out, finding the right place/editing that chain is the tricky part.

Otherwise if you are running into the issue on message.edit({ attachments: [] }), where those typings do already exist, please provide a reproducible code sample because I wasn't able to get any errors.

@Milo123459
Copy link
Contributor Author

Yea, I think it was something to do with Yarn's PnP and my workspace typescript version, that one seems to of been fixed, this PR should now focus on fixing InteractionUpdateOptions

typings/index.d.ts Outdated Show resolved Hide resolved
@Milo123459 Milo123459 changed the title fix(Typings): type attachments with never fix(Typings): type attachments to InteractionUpdateOptions Jul 21, 2021
@Milo123459
Copy link
Contributor Author

Anything else need to be changed?

@Milo123459 Milo123459 requested a review from monbrey July 21, 2021 22:19
@iCrawl iCrawl added this to the Version 13 milestone Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants