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

Typescript missing props on message #861

Closed
6 tasks
laekem34 opened this issue Mar 29, 2021 · 3 comments
Closed
6 tasks

Typescript missing props on message #861

laekem34 opened this issue Mar 29, 2021 · 3 comments
Labels
question M-T: User needs support to use the project TypeScript-specific

Comments

@laekem34
Copy link

laekem34 commented Mar 29, 2021

Description

Describe your issue here.

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • [x ] question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • [x ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x ] I've read and agree to the Code of Conduct.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Some props are missing with @slack/bolt typescript when using message listener like message.text or message.user or .team

Reproducible in: VS-code

package version: 3.3.0

node version: 15.0.1

OS version(s): window 10

Attachments:

Sans titre

@gitwave gitwave bot added the untriaged label Mar 29, 2021
@stevengill stevengill added question M-T: User needs support to use the project TypeScript-specific and removed untriaged labels Mar 29, 2021
@stevengill
Copy link
Member

Hey @laekem34!

We are aware that the current type system is lacking.

#832 landed on main and probably helps with this. It will be included in the next release (3.4.0). Essentially it allows you to specify subtypes of messages which then gives the full message props for that type.

Let me know if that helps

@seratch seratch modified the milestone: 3.4.0 Mar 29, 2021
seratch added a commit to seratch/bolt-js that referenced this issue Mar 30, 2021
@seratch
Copy link
Member

seratch commented Mar 30, 2021

Hi @laekem34, thanks for asking this question! Unfortunately, some of the fields in the message payload are not accessible without if/else statements in TypeScript.

If you want to handle only newly posted messages, checking if it has no subtype or "bot_message" (if other bot use is a "classic" app, the message events generated by it can be this) should work for you.

app.message(async ({ message }) => {
  message.channel; // the property access should compile

  // The type here is still a union type of all the possible subtyped events.
  // Thus, only the fields available for all the types can be resolved outside if/else statements.
  expectError(message.user);

  if (message.subtype === undefined) {
    expectType<GenericMessageEvent>(message);
    message.user; // the property access should compile
    message.channel; // the property access should compile
    message.team; // the property access should compile
  }
  if (message.subtype === 'bot_message') {
    expectType<BotMessageEvent>(message);
    message.user; // the property access should compile
    message.channel; // the property access should compile
  }
});

If you are interested in the complete version of the type tests, check my pull request:
https://github.com/slackapi/bolt-js/pull/862/files

@laekem34
Copy link
Author

Hello !

Thank you very much. I was a little confused on how it worked. I understand better now!
👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project TypeScript-specific
Projects
None yet
Development

No branches or pull requests

3 participants