question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infer Slack Bolt TypeScript types

See original GitHub issue

Hello, after upgrading to Slack bolt "@slack/bolt": "^3.3.0",, i am getting this TypeScript errors for types not existing.

Was trying to dig deep into Slack Bolt framework to figure out the types but to no avail so far.

Any help would be much appreciated. Thanks in advance.

 export const isThreadReply = (): Middleware<SlackEventMiddlewareArgs<'message'>> => {
  return async ({ event, next }): Promise<void> => {
    if (event.thread_ts && next) {
      await next();
    }
  };
};

Property 'thread_ts' does not exist on type 'KnownEventFromType<"message">'. Property 'thread_ts' does not exist on type 'EKMAccessDeniedMessageEvent'.ts(2339)

export const isParentMessageFromBot = (): Middleware<SlackEventMiddlewareArgs<'message'>> => {
  return async ({ context, message, next }): Promise<void> => {
    if (context.botUserId === message.parent_user_id && next) {
      await next();
    }
  };
};

Property 'parent_user_id' does not exist on type 'KnownEventFromType<"message">'. Property 'parent_user_id' does not exist on type 'BotMessageEvent'.ts(2339)

export const isNotBotMessage = (): Middleware<SlackEventMiddlewareArgs<'message'>> => {
return async ({ event, next }): Promise<void> => {
  if (!event.bot_profile && next) {
    await next();
  }
};
};

Property 'bot_profile' does not exist on type 'KnownEventFromType<"message">'. Property 'bot_profile' does not exist on type 'GenericMessageEvent'.ts(2339)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
seratchcommented, Jun 4, 2021

@erkand-imeri Can you upgrade the bolt version to the latest v3.4.0, which was just released? It should work with the version (the reason why bot_profile exists is that I added by #957 and the change is included in v.3.4.0)

1reaction
seratchcommented, Jun 3, 2021

@erkand-imeri Thanks for flagging this.

Unfortunately, the pull request https://github.com/slackapi/bolt-js/pull/709 (included in v2.6.0) changed the behavior around message events. The change is good for safer typing, but it became more confusing for users. I’m going to improve this at https://github.com/slackapi/bolt-js/issues/904 in the near future.

For this moment, if/else statements can help the TypeScript compiler understand which type should be chosen from the union type MessageEvent.

For instance, the following code compiles as event.subtype === undefined helps TS compiler determine the type of event as GenericMessageEvent.

export const isThreadReply = (): Middleware<SlackEventMiddlewareArgs<'message'>> => {
  return async ({ event, next }): Promise<void> => {
    if (event.subtype === undefined && event.thread_ts && next) {
      await next();
    }
  };
};

Similarly, the following works for the second example:

export const isParentMessageFromBot = (): Middleware<SlackEventMiddlewareArgs<'message'>> => {
  return async ({ context, message, next }): Promise<void> => {
    if (message.subtype === undefined && context.botUserId === message.parent_user_id && next) {
      await next();
    }
  };
};

But I found that even the latest revision does not work with the last example. I will include a fix for this in the next version, which we will release shortly.

Again, we’ll improve this issue at https://github.com/slackapi/bolt-js/issues/904. v3.5 or later should be working better for you. I do understand this type of error is frustrating when you use TypeScript. It’d be appreciated if you could understand the current situation around this project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using TypeScript - Slack | Bolt for JavaScript
This project is written and built using TypeScript, which means many of the APIs have type information metadata 🎉.
Read more >
slackapi/bolt-js @slack/bolt@3.11.2 on GitHub - NewReleases.io
New release slackapi/bolt-js version @slack/bolt@3.11.2 on GitHub. ... Fix #1472: say type incorrectly inferred as never when using pin_added or reaction_* ...
Read more >
Microsoft/TypeScript - Gitter
Does Typescript support scoped packages yet ? I'm trying to use this definitions files slackapi/node-slack-sdk#329 for the module @slack/client but I'm not ...
Read more >
awesome-vscode | A curated list of delightful VS Code ...
YouCompleteMe - Provides semantic completions for C/C++ (and TypeScript, ... Infer the structure of JSON and paste is as types in many programming...
Read more >
TypeScript at Slack : r/programming - Reddit
Add to that the type inference and you're left with a language that's a bit of the best of both worlds when you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found