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

c.req.query should support generics #1632

Open
kahosan opened this issue Oct 31, 2023 · 3 comments · May be fixed by #1634
Open

c.req.query should support generics #1632

kahosan opened this issue Oct 31, 2023 · 3 comments · May be fixed by #1634
Labels
enhancement New feature or request. on hold

Comments

@kahosan
Copy link

kahosan commented Oct 31, 2023

What is the feature you are proposing?

Hey

Currently, the type of query is Record<string, string>. However, when I have a predefined type, such as:

interface A {
  userId: string
  type: 'all' | 'post'
}

I hope the type of query can be changed to A, so that I can directly use c.req.query<A>() to pass parameters to functions that accept type A.

Perhaps its type definition is like this:

query<T = Record<string, string | undefined>>(): T
@kahosan kahosan added the enhancement New feature or request. label Oct 31, 2023
@yusukebe
Copy link
Member

Hi @kahosan,

This seems to be a good suggestion. However, inferring types perfectly is difficult for me, or it might be impossible.

For example, this issue

I'll continue to investigate it. Thanks.

@yusukebe yusukebe linked a pull request Oct 31, 2023 that will close this issue
3 tasks
@kahosan
Copy link
Author

kahosan commented Nov 1, 2023

right, I gave it a try.

When string exists, literals will be directly inferred as string instead of the designated literal type.
Moreover, different keys with distinct literal types will be merged.

For example:

type T = {
  a: 'a'
  b: 'b'
}

const type = query<T>('a')
//    ^? type: 'a' | 'b'

type T = {
  a: string
  b: 'b'
}

const type = query<T>('a')
//    ^? type: string

This seems to be a problem with TypeScript, do we have a way to infer the type of key?

@yusukebe
Copy link
Member

yusukebe commented Nov 1, 2023

This seems to be a problem with TypeScript, do we have a way to infer the type of key?

Hmm. I'll keep to investigating it, but it may be impossible for TypeScript (TypeScript is sometime troublesome).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request. on hold
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants