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

TypeProvider not working properly under Fastify v4 #213

Closed
2 tasks done
tajnymag opened this issue Jul 16, 2022 · 4 comments
Closed
2 tasks done

TypeProvider not working properly under Fastify v4 #213

tajnymag opened this issue Jul 16, 2022 · 4 comments

Comments

@tajnymag
Copy link

Prerequisites

  • I have written a descriptive issue title
  • 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)

Example (modified fastify/fastify-type-provider-json-schema-to-ts#12)

import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts';
import fastifyWebsocket from '@fastify/websocket';

import fastify from 'fastify';

const server = 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'],
			},
		} as const
	},
	(connection, request) => {
		const { bar } = request.query // should NOT fail
		bar.push('foo') // SHOULD fail
	}
);

server.get(
	'/http',
	{
		schema: {
			querystring: {
				type: 'object',
				properties: {
					foo: { type: 'number' },
					bar: { type: 'string' },
				},
				required: ['foo', 'bar'],
			},
		} as const
	},
	(request) => {
		const { bar } = request.query // should NOT fail
		bar.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.

@airhorns
Copy link
Member

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?

@tajnymag
Copy link
Author

Sure. I just haven't understood the typings enough yet. I'll give it another look tomorrow.

@teastman
Copy link
Contributor

I'm having the same issues, I'll try my hand at a PR.

@tajnymag
Copy link
Author

The issue has been fixed by #225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants