We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
It would be beneficial to have consistent Router types for the request.
Currently, we have the following types for Router and AutoRouter:
export const Router = < RequestType = IRequest, Args extends any[] = any[], ResponseType = any >(
export const AutoRouter = < RequestType extends IRequest = IRequest, Args extends any[] = any[], ResponseType = any >({
The difference is that AutoRouter and IttyRouter require RequestType to be a subtype of IRequest, while Router does not.
The text was updated successfully, but these errors were encountered:
To address the issue with Cloudflare types, especially the request.cf.xxx properties, I suggest the following changes:
Remove the extend keyword and use the current Router types, or adjust IRequest slightly.
The problem arises because the default worker Request type causes every cf property to have an unknown type, like this:
type MyContinent = NonNullable<Request['cf']>["continent"]; // <-- unknown type MyContinentGood = NonNullable<Request<unknown, IncomingRequestCfProperties<unknown>>['cf']>["continent"]; // <-- ContinentCode | undefined
More details
Cloudflare Pages already use Request<unknown, IncomingRequestCfProperties> as its request type (so no issues with cf.props), but workers do not.
Using itty-router, defining the router like this yields the best results:
const router = Router< Request<unknown, IncomingRequestCfProperties<unknown>> >({...}); router.get('*', (request) => { const code = request.cf.continent; // <-- code has ContinentCode | undefined type });
However, this definition is impossible with AutoRouter because it requires Request<unknown, IncomingRequestCfProperties> to extend IRequest.
The solution is to remove extends in all routers at RequestType extends IRequest and/or also make IRequest generic, like so:
RequestType extends IRequest
type IRequest<TRequest = Request> = { route: string; params: { [key: string]: string; }; query: { [key: string]: string | string[] | undefined; }; proxy?: any; } & TRequest & ...;
This adjustment will allow for better type handling and compatibility with Cloudflare’s request.cf.xxx properties.
Sorry, something went wrong.
No branches or pull requests
Describe the Issue
It would be beneficial to have consistent Router types for the request.
Currently, we have the following types for Router and AutoRouter:
The difference is that AutoRouter and IttyRouter require RequestType to be a subtype of IRequest, while Router does not.
The text was updated successfully, but these errors were encountered: