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

Setting reply_broadcast gives a type error when it's a dynamic value #1859

Closed
1 of 7 tasks
Amethystix opened this issue Jul 30, 2024 · 2 comments · Fixed by #1860
Closed
1 of 7 tasks

Setting reply_broadcast gives a type error when it's a dynamic value #1859

Amethystix opened this issue Jul 30, 2024 · 2 comments · Fixed by #1860
Labels
area:typescript issues that specifically impact using the package from typescript projects bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented

Comments

@Amethystix
Copy link

Packages:

Select all that apply:

  • @slack/web-api
  • @slack/rtm-api
  • @slack/webhooks
  • @slack/oauth
  • @slack/socket-mode
  • @slack/types
  • I don't know

Reproducible in:

The Slack SDK version

v7.3.1

Node.js runtime version

18.18.2

Typescript version

5.5.4

OS info

ProductName:		macOS
ProductVersion:		13.3.1
ProductVersionExtra:	(a)
BuildVersion:		22E772610a
Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000

Steps to reproduce:

Dynamically set reply_broadcast as we do in this wrapper function:

  async reply(
    channelId: string,
    message: Block[] | string,
    threadId: string,
    broadcast = false,
  ) {

    if (typeof message === "string") {
      return await this.web.chat.postMessage({
        channel: channelId,
        thread_ts: threadId,
        text: message,
        reply_broadcast: broadcast,
      });
    } else {
      return await this.web.chat.postMessage({
        channel: channelId,
        thread_ts: threadId,
        reply_broadcast: broadcast,
        blocks: message,
      });
    }
  }

Expected result:

No type error

Actual result:

Argument of type '{ channel: string; thread_ts: string; reply_broadcast: boolean; blocks: KnownBlock[]; }' is not assignable to parameter of type 'ChatPostMessageArguments'.
  Type '{ channel: string; thread_ts: string; reply_broadcast: boolean; blocks: KnownBlock[]; }' is not assignable to type 'TokenOverridable & ChannelAndBlocks & BroadcastedThreadReply & { as_user: true; icon_emoji?: undefined; icon_url?: undefined; } & ... 4 more ... & { ...; }'.
    Type '{ channel: string; thread_ts: string; reply_broadcast: boolean; blocks: KnownBlock[]; }' is not assignable to type 'BroadcastedThreadReply'.
      Types of property 'reply_broadcast' are incompatible.
        Type 'boolean' is not assignable to type 'true'.

Note: if I set reply_broadcast to true or false the error goes away.

Requirements

For general questions/issues about Slack API platform or its server-side, could you submit questions at https://my.slack.com/help/requests/new instead. 🙇

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

@filmaj filmaj added needs info An issue that is claimed to be a bug and hasn't been reproduced, or otherwise needs more info bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented area:typescript issues that specifically impact using the package from typescript projects and removed untriaged needs info An issue that is claimed to be a bug and hasn't been reproduced, or otherwise needs more info labels Jul 31, 2024
@filmaj
Copy link
Contributor

filmaj commented Jul 31, 2024

I can reproduce this but I'm not sure how to fix this without loosening up the argument typing. Here's a TypeScript Playground link for the reproduction.

For reference, here are the three interfaces that govern this issue: one interface modeling thread_ts, and then two others that model the situation for when reply_broadcast is true vs. false:

interface ThreadTS {
  /**
   * @description Provide another message's `ts` value to post this message in a thread. Avoid using a reply's `ts`
   * value; use its parent's value instead.
   */
  thread_ts: string;
}
interface WithinThreadReply extends Partial<ThreadTS> {
  /**
   * @description Used in conjunction with `thread_ts`, when set to `false` will make the reply only visibile within
   * a thread.
   */
  reply_broadcast?: false;
}
interface BroadcastedThreadReply extends ThreadTS {
  /** @description Used in conjunction with `thread_ts`, when set to `true` will broadcast the reply to the channel. */
  reply_broadcast: true;
}

Assigning a value to the boolean makes the issue go away... Not sure why this is the case.. not a TypeScript expert but I assumed that modeling both true and false values for boolean (via the WithinThreadReply and BroadcastedThreadReply interfaces) would be sufficient to model all possible values of the boolean type. I guess TypeScript is not smart enough to figure that out in the case there is no specific value attached to the reply_broadcast boolean?

Open to any suggestions on how to address this... one approach would be to loosen up BroadcastedThreadReply interface's reply_broadcast type from true to boolean. I have a PR implementing that here: #1860

filmaj pushed a commit that referenced this issue Jul 31, 2024
@filmaj filmaj added this to the [email protected] milestone Jul 31, 2024
@filmaj
Copy link
Contributor

filmaj commented Jul 31, 2024

This has been fixed in the just-released web-api v7.3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typescript issues that specifically impact using the package from typescript projects bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants