slackapi / node-slack-sdk

Slack Developer Kit for Node.js

Home Page:https://slack.dev/node-slack-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why is `code` property not included in RichTextStyleable type?

AleksaStevic opened this issue · comments

Rich text section can contain a list of rich text elements. The most common is element of type text - RichTextText type. We can add style property to this element to change the appearance of the text:

style?: {
    bold?: boolean;
    italic?: boolean;
    strike?: boolean;
    highlight?: boolean;
};

In the documentation and block editor, it is allowed to add code property to style to make the text appear as inline code, although in the RichTextStyleable type it is excluded, and there is a comment that it is excluded but I don't know why:
https://github.com/slackapi/node-slack-sdk/blob/main/packages/types/src/block-kit/extensions.ts#L56

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

"slack/bolt": "^3.16.0",
"slack/web-api": "^6.10.0",

"@slack/bolt" has a dependency: "@slack/types": "^2.9.0" that is used for typings.

Node.js runtime version

v20.10.0

OS info

ProductName: macOS
ProductVersion: 14.2
BuildVersion: 23C64
Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020

Steps to reproduce:

(Share the commands to run, source code, and project settings)

Using @slack/bolt, in any message handler add this code:

  1. tsconfig.json compiler options:
"strict": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitThis": true,
"allowSyntheticDefaultImports": true
  1. Example code in plain @slack/bolt project:
const msg = await say({
  text: 'Searching for answers...',
  blocks: [
	{
		type: 'rich_text',
		elements: [
			{
				type: 'rich_text_section',
				elements: [
					{
						type: 'text',
						text: 'Hello, I should be a code',
						style: {
							code: true,
						},
					},
				],
			},
		],
	},
  ],
})
  1. run tsc --noEmit --skipLibcheck

Expected result:

No type issues.

Actual result:

TS error: Type '{ type: "text"; text: string; style: { code: true; }; }' is not assignable to type 'RichTextElement'

I also get red lines in the editor immediately.

The issue is that code property is not included in RichTextStyleable['style'], but should be.

Hey @AleksaStevic, thanks for the issue, you are totally correct.

I just tried in testing, and indeed you can set style: { code: true } on text elements to do inline code styling. As a workaround, you can simply set this property on any message-posting API (like chat.postMessage and its blocks argument) and the message will be posted with the correct styling. If you are using a typescript library, until we release a fix for this, you will have to likely use the // @ts-expect-error directive to have TS ignore the use of an unsupported type field.

Also, in case it is helpful: the preformatted code blocks do work and are typed, see: https://github.com/slackapi/node-slack-sdk/blob/main/packages/types/src/block-kit/block-elements.ts#L952

I will try to whip up a PR to address this issue.

This is a specific use case where I need inline code to be typed correctly. I can't use preformatted. I can create a quick PR to fix this if you want.

@AleksaStevic I created #1707 to address this, I will try to make a new release of @slack/types today.

Are you using the types via any other of the node SDKs? Like @slack/web-api or @slack/bolt?

@filmaj Yes, @slack/bolt re-exports all types from "@slack/types" and I am using that, also, Bolt functions are typed using @slack/types. So it's required to upgrade the @slack/types dependency in @slack/bolt when version @slack/types is released with this fix.

If that's not possible, I suppose I could use something like overrides or resolutions in package.json to override the dependency version.

It's fine, just making sure so I know which packages to include the updates to. As soon as I can get a review on #1707 I will release all three: types, web-api and bolt.

Going to start the release process, I'll post here once the new versions are up.

@slack/types v2.11 is live, which is included in the newly-released @slack/web-api v6.11. Both of these are also now included in the newly-released @slack/bolt v3.17.

Happy holidays!