You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.2.1
Plugin version
6.0.1
Node.js version
16.13.2
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
10
Description
Fastify-websocket seems to break TypeProvider typings in its WebsocketHandler. I am able to compile code with the official json-schema-to-ts type-provider. After enabling websocket: true in the route's options and changing the route's handler, the type inference vanishes.
In my example, the /websocket route fails to compile due to request.query being typed as unknown. It should behave the same way the handler in /http route does. To infer the query params and fail due to a type mismatch on the following line.
Steps to Reproduce
Try running this code with a modern typescript version. Reproducible even on TypeScript Playground (link to prepared playground)
import{JsonSchemaToTsProvider}from'@fastify/type-provider-json-schema-to-ts';importfastifyWebsocketfrom'@fastify/websocket';importfastifyfrom'fastify';constserver=fastify().withTypeProvider<JsonSchemaToTsProvider>();server.register(fastifyWebsocket);server.get('/websocket',{websocket: true,schema: {querystring: {type: 'object',properties: {foo: {type: 'number'},bar: {type: 'string'},},required: ['foo','bar'],},}asconst},(connection,request)=>{const{ bar }=request.query// should NOT failbar.push('foo')// SHOULD fail});server.get('/http',{schema: {querystring: {type: 'object',properties: {foo: {type: 'number'},bar: {type: 'string'},},required: ['foo','bar'],},}asconst},(request)=>{const{ bar }=request.query// should NOT failbar.push('foo')// SHOULD fail});
Expected Behavior
Generally, the types should stay inferred like in any other http handler.
In my example, the route /websocket should have the same compilation error like the route /http. That means request.query should be correctly inferred to be an object of { foo, bar } and bar.push('foo') should fail as bar isn't an array.
The text was updated successfully, but these errors were encountered:
Agreed -- the types in fastify-websocket may need updating to properly proxy through the type provider when it infers the new route handler signature. Want to take a crack at a PR?
Prerequisites
Fastify version
4.2.1
Plugin version
6.0.1
Node.js version
16.13.2
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
10
Description
Fastify-websocket seems to break TypeProvider typings in its WebsocketHandler. I am able to compile code with the official json-schema-to-ts type-provider. After enabling
websocket: true
in the route's options and changing the route's handler, the type inference vanishes.In my example, the
/websocket
route fails to compile due torequest.query
being typed asunknown
. It should behave the same way the handler in/http
route does. To infer the query params and fail due to a type mismatch on the following line.Steps to Reproduce
Try running this code with a modern typescript version. Reproducible even on TypeScript Playground (link to prepared playground)
Example (modified fastify/fastify-type-provider-json-schema-to-ts#12)
Expected Behavior
Generally, the types should stay inferred like in any other http handler.
In my example, the route
/websocket
should have the same compilation error like the route/http
. That meansrequest.query
should be correctly inferred to be an object of{ foo, bar }
andbar.push('foo')
should fail asbar
isn't an array.The text was updated successfully, but these errors were encountered: