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
Add types to async fastify hooks #2615
Conversation
Can't we use overloads? example: /**
* `onRequest` is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be `preParsing`.
* Notice: in the `onRequest` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
*/
export interface onRequestHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
} |
@L2jLiga unfortunately it doesn't seem to work :( |
What's the error? |
Error was on the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add tests first and then check what is better approach
I tried the approach of overload but seems not works. However, I'll update the PR with this approach to show. |
I just checked both approaches with TS playground, neither separated declaration nor inside one interface seems to work update: seems like I found possible solution, @fox1t wdyt? |
If you try this in the project the issue comes at the end of the different hooks. Like the Also in your example:
Means like the type mapping is not working properly |
Yeah, it's just a warning that they're not used hence can be deleted
This is expected due to documentation
|
I updated example in playground: link update: link was updated |
My bad you're right. This looks like a good alternative |
@RafaelGSS, @fox1t what is your opinion on it? |
@L2jLiga, this is a brilliant solution! Conditional types allow inferring the proper types instead of relying upon intersection/overloading. As we (re)discovered here, we can't use overloading (type intersection in the types world) for fastify methods. We already encountered this same issue when we decided to deprecate the |
I definitely like this. @Ethan-Arrowood what do you think?
|
Ooh very clever. Inferring the function type (callback v async) based on the arguments list. I like it! |
this does not make much sense for example, so no answer 😅
Unfortunately I have no idea how to force ts to check arguments length 😕
I think would be better to describe it block by block, I will try provide description asap 🐱 |
I don't think you can. I believe TS specifically doesn't do this for some other reasons. Honestly, I can't remember exactly but I do think I explored that rabbit hole in the past and came up empty. |
Hey! I'll update the example tonight with the example provided by @L2jLiga! Seems very interesting! |
@L2jLiga what is |
Then I suggest to don't worry about :)
Cool! Thanks dude!
Just an example how it can be used for hooks with different arguments length, does not related to fastify itself |
@L2jLiga I have failed to update the |
@mcollina we are waiting for the description of the code provided by @L2jLiga. After that, we can try to integrate his solution in fastify types. If it works we finally have a way of expressing polymorphic methods and it would be an awesome achievement. |
Fixed (Sorry for the late - busy week). If the implementation is accepted I'll change every hook. |
@L2jLiga Unfortunately the implementation seems not to work on |
@RafaelGSS I mentioned this here and already thinking how we can fix it 🤔 |
Here's fix: |
I think we should just wait for the TS 4.1 if we want to implement this. It is a breaking change for TS users. |
@fox1t This does not change only on fastify side? IMHO we should land this fix without the overload/conditional type and after the |
Yes. Sorry for the confusion: I meant that we should wait to land the "overload/conditional type" part and go on with the current fix for the moment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, works for me :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@Ethan-Arrowood Have any concerns about? I will land tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm :) great work everyone!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I did not add types for non-documented async hooks like:
onRegister
hook. I would like to adjust it in another PR.This PR solve this kind of usage:
Checklist
npm run test
andnpm run benchmark
and the Code of conduct