slackapi / slackapi/bolt-js

Document extending context using `App<CustomContext>` generic

Open
#1,362 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

docs TypeScript-specific
Dominant language
TypeScript
Stars
2.9k
Forks
445
Avg merge
1d 3h
Merged PRs (30d)
28

Description

Description

In the types files for context that gets passed around, it's mentioned that we can add stuff to the context as a way of passing around information. Express does something similar with their Request/Response/Application, but they specifically allow you to extend their interfaces to add your own typed properties. For typescript, this is very handy.

Here's there global namespace that makes it possible: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/index.d.ts#L15

Here's how you can add custom typed properties in a consuming app

declare global {
  namespace Express {
    interface Request {
      context: Context;
      foo: Foo;
    }
  }
}

which is really nice because the autocomplete works and it knows all my typings
image

image

What type of issue is this? (place an x in one of the [ ])
  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion
Requirements (place an x in each of the [ ])
  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Expected result:

Ability to add types to properties that have been defined in the global namespace to things that are typically passed through handlers, Context would be my suggestion, but other interfaces would also be welcome.

Extending the interface like above should then smartly apply the correct types to extra properties that people define.

Actual result:

Types are not applied today. Any extra properties assigned to the context variable are typed any. To get typing back, we have to force casting to a class/interface.

Attachments:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript context type files mentioned in the issue and compare their extension needs with Express's Request/Response/Application declarations. Define how consumers should provide typed custom context properties, then verify that handler context properties receive those types and no longer fall back to any.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.