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

Add this types to decorator functions #3203

Merged
merged 4 commits into from Jul 21, 2021
Merged

Conversation

matthyk
Copy link
Contributor

@matthyk matthyk commented Jul 17, 2021

Checklist

Just added the correct type for the this binding in the decorator functions. Additionally I added a note in the documentation that an arrow function in the decorate function would break the this binding.

@Eomm Eomm added the typescript TypeScript related label Jul 17, 2021
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina requested a review from a team July 18, 2021 06:56
Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think any of the type merge with any will become any.

@@ -130,3 +136,13 @@ expectType<string>(server.printRoutes({ includeHooks: true, commonPrefix: false,
expectType<string>(server.printRoutes({ includeMeta: ['key1', Symbol('key2')] }))

expectType<string>(server.printRoutes())

server.decorate('test', function (x: string) {
expectType<FastifyInstance | any>(this)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is FastifyInstance | any, I think it would become any at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right. But then the tests are actually useless, aren't they?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do expectType<FastifyRequest>(this)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, the test then fails. I guess the problem is that the type of the decorator value is types as any.

  decorate(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, any overload on top of any the type will be merged to becomes any. So, either change the any to something generic without Function or this PR should be no actual gain.

@github-actions github-actions bot removed the typescript TypeScript related label Jul 20, 2021
@matthyk
Copy link
Contributor Author

matthyk commented Jul 20, 2021

I have added generics to the 3 decorator functions. This can now be used to check whether the value to be decorated is a function, and if so, the correct this type is added.

const server = fastify()

server.decorate<(n: number) => void>('myDecorator', function(n: number): void {
  this // has FastifyInstance type now
})

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of Generic types for solving this issue. Can you add an test-case for not specify the types? Generic suppose to works without explicit specification.

For example the below test should works.

server.decorate('test', function (x: string): void {
  expectType<FastifyInstance>(this)
})

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Amazing approach!

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one more thing. Add both test case for decorateRequest and decorateReply without Generic specification. Then it is all good.

Just to ensure three of them can use with or without explicit specification.

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks a lot.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants