question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get full type safety with extracted handler function?

See original GitHub issue

Below 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:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
yusukebecommented, Oct 29, 2022

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:

interface Handler<
  P extends string | Partial<Environment> | Partial<Schema>,
  E = Partial<Environment> | Partial<Schema>,
  S = Partial<Schema>
> {
  (
    c: Context<
      P extends string
        ? P
        : P extends Partial<Environment>
        ? string
        : P extends Partial<Schema>
        ? string
        : string,
      P extends Partial<Environment> ? P : E extends Partial<Environment> ? E : Environment,
      S extends Schema ? S : Schema
    >
  ): Response
}

Then, we can pass the Generics to Handler in three ways:

const handler1: Handler<'id', Env, Schema> = (c) => {
  const id = c.req.param('id')
  const foo = c.get('foo')
  const { query } = c.req.valid()
  return c.text('Hi')
}
SS

There is no need to write string at the beginning when we want to pass only Env:

const foo: Handler<Env, Schema> = (c) => {
  const foo = c.get('foo')
  const { query } = c.req.valid()
  return c.text('Hi')
}
SS

We can also pass the Schema type created from Validator:

const bar: Handler<Schema> = (c) => {
  const { query } = c.req.valid()
  return c.text('')
}
SS

Full code: https://gist.github.com/yusukebe/cb666ab0a89faf1fd8975dff6b33a7a7

I think it’s a cool feature. I may adapt it to production code.

1reaction
yujong-leecommented, Oct 29, 2022

@yusukebe

Wow. This Handler type is something that I was looking for.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found