-
Notifications
You must be signed in to change notification settings - Fork 398
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
No way to get at concrete Event Types #2316
Comments
OK. The solution is to drop Typescript, JSON.stringify the args into something like this:
And then get the text with something like: |
Hi @alper, thank you for writing in. We do understand your frustration with the message event listener types in TypeScript. The message event has several variations, which is why the type can be confusing, especially for new Slack app developers. The payload argument's type is a union data type that consists of these subtypes: https://api.slack.com/events/message#subtypes To safely determine a specific type in TypeScript, the quickest way is to use if/else statements to check what the subtype is (or if it does not exist), as shown below: import { App, EventFromType } from '@slack/bolt';
import { GenericMessageEvent, MessageChangedEvent } from "@slack/types";
app.event("message", async ({ payload }) => {
// this one is still a union type of subtyped message events
const variousMessageEventPayload: EventFromType<"message"> = payload;
if (!payload.subtype) {
// newly posted messages from a human do not have sutype
// inside if block, the data type can be safely determined
const postedMessageEventPayload: GenericMessageEvent = payload
} else if (payload.subtype === "message_changed") {
// there are a few other patterns; see https://api.slack.com/events/message#subtypes
const messageChangedEventPayload: MessageChangedEvent = payload;
}
}); This is what the past issues you've already linked try to describe and improve for the developer experience. We still cannot tell when and how this can be improved to be more intuitive, as the improvements we're considering are a breaking change to existing apps. This library is written in TS, and users can use it in either JS or TS. Here is a project template: https://github.com/slack-samples/bolt-ts-starter-template This message event handling is the most confusing. Other listener code should not be like that. Hope you'll like this library once you become familiar with it. |
Ah, I need: That wasn't listed anywhere. I'd think it'd be best to re-export them from this package for convenience. |
Most users don't need to explicitly refer to the types, so they're not mentioned in the documents. However, re-exporting the types from |
OK. Cool but it does work either way with the types or with |
Not sure what's happening, I have:
But
GenericMessageEvent
is not exported?I see no real way to go from
KnownEventFromType<'message'>
to an actualSlackEvent
. What's the point here?Similar to #904 and #955
The text was updated successfully, but these errors were encountered: