You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
The text was updated successfully, but these errors were encountered:
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
changed the title
Add global namespace so we can extend context/other args types
Document extending context using App<CustomContext> generic
Sep 15, 2024
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 theirRequest
/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
which is really nice because the autocomplete works and it knows all my typings
What type of issue is this? (place an
x
in one of the[ ]
)Requirements (place an
x
in each of the[ ]
)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 typedany
. To get typing back, we have to force casting to a class/interface.Attachments:
The text was updated successfully, but these errors were encountered: