Skip to content

Commit

Permalink
add distributiveomit utility type and use it for say and respond argu…
Browse files Browse the repository at this point in the history
…ments. fix up getting started typescript example
  • Loading branch information
Filip Maj committed Sep 14, 2024
1 parent a6230e5 commit 7e07e30
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/getting-started-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"dependencies": {
"@slack/bolt": "^3.3.0",
"dotenv": "^8.2.0"
"dotenv": "^16"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/getting-started-typescript/src/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ app.action<BlockAction>({ action_id: 'select_user', block_id: 'assign_ticket' },
await client.reactions.add({
name: 'white_check_mark',
timestamp: body.message?.ts,
channel: body.channel?.id,
channel: body.channel!.id, // if the body has a message, we know it has a channel, too.
});
}
} catch (error) {
Expand Down
11 changes: 7 additions & 4 deletions src/types/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ export const isFulfilled = <T>(p:PromiseSettledResult<T>): p is PromiseFulfilled
/** Type predicate for use with `Promise.allSettled` for filtering for rejected results. */
export const isRejected = <T>(p:PromiseSettledResult<T>): p is PromiseRejectedResult => p.status === 'rejected';

// TODO: consider moving the following types closer to relevant code
/** Using type parameter T (generic), can distribute the Omit over a union set. */
type DistributiveOmit<T, K extends PropertyKey> = T extends any
? Omit<T, K>
: never;

// The say() utility function binds the message to the same channel as the incoming message that triggered the
// listener. Therefore, specifying the `channel` argument is not required.
export type SayArguments = Omit<ChatPostMessageArguments, 'channel'> & {
export type SayArguments = DistributiveOmit<ChatPostMessageArguments, 'channel'> & {
// TODO: This will be overwritten in the `createSay` factory method in App.ts anyways, so why include it?
channel?: string;
};

export interface SayFn {
(message: string | SayArguments): Promise<ChatPostMessageResponse>;
}

export type RespondArguments = Omit<ChatPostMessageArguments, 'channel' | 'text'>
export type RespondArguments = DistributiveOmit<ChatPostMessageArguments, 'channel' | 'text'>
& {
/** Response URLs can be used to send ephemeral messages or in-channel messages using this argument */
response_type?: 'in_channel' | 'ephemeral';
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declarationMap": true,
"module": "CommonJS",
"moduleResolution": "node",
"noErrorTruncation": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
Expand Down
2 changes: 1 addition & 1 deletion types-tests/action.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectError, expectType } from 'tsd';
import { App, BlockElementAction, InteractiveAction, DialogSubmitAction } from '../';
import { App, BlockElementAction, InteractiveAction, DialogSubmitAction } from '..';

const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' });

Expand Down
4 changes: 3 additions & 1 deletion types-tests/utilities.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expectType } from 'tsd';
import { ChatPostMessageResponse } from '@slack/web-api';
import { App, InteractiveButtonClick } from '..';
// eslint-disable-next-line
import App from '../src/App';
import { InteractiveButtonClick } from '../src/types';

const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' });

Expand Down

0 comments on commit 7e07e30

Please sign in to comment.