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

fix: propagate generics from FastifyRegister to plugin type #4034

Merged
merged 7 commits into from Jun 19, 2022
Merged

fix: propagate generics from FastifyRegister to plugin type #4034

merged 7 commits into from Jun 19, 2022

Conversation

stefee
Copy link
Contributor

@stefee stefee commented Jun 17, 2022

Closes #4032

Checklist

@github-actions github-actions bot added the typescript TypeScript related label Jun 17, 2022
@stefee stefee changed the title Propagate generics from FastifyRegister to plugin type fix: propagate generics from FastifyRegister to plugin type Jun 17, 2022
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.

Can you include a tsd test as well?

@stefee
Copy link
Contributor Author

stefee commented Jun 17, 2022

@RafaelGSS Yep, will do when I have time.

@stefee stefee marked this pull request as ready for review June 17, 2022 17:57
@stefee stefee requested a review from RafaelGSS June 17, 2022 17:58
@stefee
Copy link
Contributor Author

stefee commented Jun 17, 2022

Fixed commit message.

@stefee stefee marked this pull request as draft June 17, 2022 18:19
@stefee
Copy link
Contributor Author

stefee commented Jun 17, 2022

Changing back to a draft - there's a failing test.

@stefee
Copy link
Contributor Author

stefee commented Jun 17, 2022

The failing test is in hook.test-d.ts

https://github.com/stefee/fastify/blob/e68bd8c42b5cbcabcb4cfee860e1e8d87a730b9c/test/types/hooks.test-d.ts#L217-L236

test/types/hooks.test-d.ts:235:19
  ✖  235:19  No overload matches this call.
  The last overload gave the following error.
    Argument of type "preHandler" is not assignable to parameter of type "onClose".

  1 error

The problem is to do with passing anonymous (and therefore implicitly typed) plugin into .register(. It seems that the type of instance that gets passed to the plugin has a concrete type which has changed as a result of the change in this PR.

@stefee stefee marked this pull request as ready for review June 17, 2022 18:37
@stefee
Copy link
Contributor Author

stefee commented Jun 17, 2022

The above issue was resolved by providing default values inferred from the instance on which .register is called.

@stefee
Copy link
Contributor Author

stefee commented Jun 17, 2022

Moved tests into the relevant file.

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.

@RafaelGSS
Copy link
Member

cc: @sinclairzx81

types/register.d.ts Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

@climba03003 @fox1t can you take a look?

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 am only afraid will it affect the non-default generic.
For example, people using http2 or https.

@mcollina
Copy link
Member

@sinclairzx81 do you approve this?

@sinclairzx81
Copy link
Contributor

@mcollina Hi, this looks ok to me. As suggested by @climba03003, it may be good to put some TS tests up around the http2 and https server types (as these propagate), This would just rule out any issues with varying server types prior to merge if there are concerns there.

But other than that, this looks great :) @stefee Nice work!

@stefee
Copy link
Contributor Author

stefee commented Jun 18, 2022

@climba03003 @sinclairzx81 @mcollina Which type signature is expected for a https server instance?

default:

FastifyInstance

http2:

FastifyInstance<Http2Server, Http2ServerRequest, Http2ServerResponse>

with type provider:

FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance, TestTypeProvider>

https: ?

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

@sinclairzx81
Copy link
Contributor

Which type signature is expected for a https server instance?

@stefee The following generic arguments should match each of the varying server types.

import * as http from 'http'
import * as https from 'https'
import * as http2 from 'http2'

...

// With Http
const serverWithHttp = fastify({ })
type ServerWithHttp = FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>

...

// With Https
const serverWithHttps = fastify({ https: { } })
type ServerWithHttps = FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>

...

// With Http2
const serverWithHttp2 = fastify({ http2: true })
type ServerWithHttp2 = FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
...

Have tested the HTTPS variant locally, and all looks good to me :)

Good work!

@fox1t
Copy link
Member

fox1t commented Jun 19, 2022

LGTM! Nice job!

@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 Jun 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript TypeScript related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FastifyRegister type does not propagate type provider generic to plugin type
6 participants