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

Document extending context using App<CustomContext> generic #1362

Open
4 of 10 tasks
asherbig opened this issue Mar 10, 2022 · 2 comments
Open
4 of 10 tasks

Document extending context using App<CustomContext> generic #1362

asherbig opened this issue Mar 10, 2022 · 2 comments
Labels
docs M-T: Documentation work only TypeScript-specific
Milestone

Comments

@asherbig
Copy link

asherbig commented Mar 10, 2022

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:

@seratch seratch added enhancement M-T: A feature request for new functionality TypeScript-specific labels Mar 10, 2022
@seratch seratch added this to the 3.x milestone Mar 10, 2022
@seratch
Copy link
Member

seratch commented Mar 10, 2022

Hi @asherbig, thanks a lot for taking the time to share this feedback!

The pull request #1157 is trying to improve this in a bit different way. We are unable to tell when and how some improvement can be added in the future but I do understand the enhancement around the type of context object for TypeScript use cases.

@filmaj filmaj removed this from the 3.x milestone Sep 15, 2024
@filmaj filmaj added docs M-T: Documentation work only and removed enhancement M-T: A feature request for new functionality labels Sep 15, 2024
@filmaj filmaj changed the title Add global namespace so we can extend context/other args types Document extending context using App<CustomContext> generic Sep 15, 2024
@filmaj
Copy link
Contributor

filmaj commented Sep 15, 2024

I've changed this issue to be about documenting how to extend the context object using the CustomContext generic already present on the App class.

@filmaj filmaj added this to the 4.x milestone Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs M-T: Documentation work only TypeScript-specific
Projects
None yet
Development

No branches or pull requests

3 participants