How to get full type safety with extracted handler function?
See original GitHub issueBelow is minimal example that I am curious about.
app.get(`/api/:id`, (c) => {
const key = c.req.param().id; // This is auto-completed
});
app.get(`/api/:id`, handler);
const handler = (c: Context<string, Environment>) => {
const key = c.req.param().id; // This is NOT auto-completed
}
Could you guide me how to deal with this? Thanks.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
typescript - Type Error when trying to extract type from unions
I am trying to add type definitions to some code that uses message passing over web workers. The dispatched messages have a string...
Read more >Make the translation function fully type-safe · Issue #1504
It's probably possible to use TypeScript template literal types to extract types from the message string, but I haven't seen anyone experiment ...
Read more >Pattern matching and type safety in TypeScript
The function allows us to match on a tag of a value of type Either<string> . Right away we get a hint on...
Read more >Handling Safe Destructuring in TypeScript
There we go; we've totally removed the need for destructuring with this function by simplifying the requirements and avoiding mutation of ...
Read more >Type-Safe TypeScript
Fortunately, with some non-default configuration and some discipline, it is possible to get a substantial degree of both compile-time and run- ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @yujong-lee !
Thanks for the detail you shared with us! You’re right, but there’s a lot of work to do.
So I tried another way to pass the Generics to “Handler” easily, and it was not so difficult.
@yujong-lee @ThatOneBro
This is just PoC, but it works. Define the
Handler
as below:Then, we can pass the Generics to
Handler
in three ways:There is no need to write
string
at the beginning when we want to pass onlyEnv
:We can also pass the
Schema
type created fromValidator
:Full code: https://gist.github.com/yusukebe/cb666ab0a89faf1fd8975dff6b33a7a7
I think it’s a cool feature. I may adapt it to production code.
@yusukebe
Wow. This
Handler
type is something that I was looking for.