-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Default type for Context and Handler prevents extension or override #583
Comments
Handler must have 3 parameters: context, Request,Response. You can send your own parameter after those ones. f.e. async handler(ctx: Context, req: Request, res: Response, myObject: {id: string}, mySecondObject: ...) |
That would be true if i were using express. my project is using koa, which only has 2 arguments going into the handler. |
The error you are getting indicates that there's some kind of disjunction in typing since your custom type UnknownParams = {
[key: string]: string | string[];
}
export type Handler<
RequestBody = any,
Params = UnknownParams,
Query = UnknownParams,
Headers = UnknownParams,
Cookies = UnknownParams,
D extends Document = Document,
> = (context: Context<RequestBody, Params, Query, Headers, Cookies, D>, ...args: any[]) => any | Promise<any>; Looking at the public register(handlers: { [operationId: string]: Handler }): void; Your custom It seems to me that we could overcome this issue by making the registerHandler<
RequestBody = any,
Params = UnknownParams,
Query = UnknownParams,
Headers = UnknownParams,
Cookies = UnknownParams,
D extends Document = Document
>(
operationId: string,
handler: Handler<
RequestBody = any,
Params = UnknownParams,
Query = UnknownParams,
Headers = UnknownParams,
Cookies = UnknownParams,
D extends Document = Document
>
): void; Circling back to the example provided in the description: type Body = unknown
type PathParams = { id: string }
const myHandler: Handler<Body, PathParams> = (
context,
ctx: KoaContext,
) => {}
api.registerHandler<Body, PathParams >('myHandler', myHandler) Doing so connects the dots for TypeScript and will prevent type mismatch complaints. @anttiviljami curios to hear your thoughts on the matter |
if i define a handler like so, and attempt to register it with other handlers, tsc will throw errors.
the errors
what am i doing wrong here?
The text was updated successfully, but these errors were encountered: