-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Release 3.1.0 broke type inference on fastify route handler request parameter #73
Comments
I think this could be a Typebox version compatibility issue. I ran into something similar, import { TypeBoxTypeProvider, Type } from '@fastify/type-provider-typebox';
// Do not import @sinclair/typebox directly
import fastify from 'fastify';
fastify()
.withTypeProvider<TypeBoxTypeProvider>()
.get('/', {
schema: {
params: Type.Object({
foo: Type.String()
})
},
handler(req, rep) {
console.log(req.params.foo);
rep.send();
}
}); |
I'm experiencing the same problem - changed import to |
@sinclairzx81 any guidance here? Experiencing this upgrading from *27.8 to 28.5 |
@semoal Hi! In 3.0, TypeBox was taken as a direct dependency on the provider (where as previously it was a peerDependency). If manually upgrading to TypeBox 0.28.x (or versions higher than the dependency used by the provider) this may be the cause type inference problems as TypeScript may be observing both installed versions of TypeBox (leading to possible type inference ambiguities). There's a couple of options I can think of to resolve this. A) Upgrade the provider to use TypeBox I think option B might be the most flexible option for sourcing the most recent TB revisions. @mcollina @RafaelGSS Any thoughts on this? |
Hopefully, seems like the patches in progress. Just sharing my use: I always write a dedicated import to TypeBox from the actual TypeBox package ( Also, the library should be peer dependency in my case. |
Since I personally prefer reverting back to For others looking for temporary solution, in the meantime, you can keep using 3.1.0 by version pinning your @sinclair/typebox dependency to 0.27.8 (but I also recommend also performing exact version pinning to 3.1.0 in case fix is being rolled out later for your project stability). |
I'd go with option B. However, I'm not sure it will solve all the edge cases. |
+1 |
@mcollina okay to release 3.3.0 and close this? |
yes |
@nadhifikbarw I'm lost in what should be in v3.3.0, could you clarify? |
I think this issue was solved in v3.2.0. |
yeah, there is no need for a release, the CI is already updated! Thx for your help! |
I seem to be experiencing this issue again: import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
// ...
fastify .get(
"/:id",
{
schema: {
params: Type.Object({
_id: Type.String(),
}),
response: {
200: UserRef,
404: ErrorResponseRef,
},
description: "Get a user by ID",
operationId: "getUserById",
tags: ["Users"],
},
preHandler: [is_authenticated()],
},
async (request) => {
const user = await users_service.getById(request.params._id); // 'request.params' is of type 'unknown'.ts(18046)
// ...
}
) The only solution I've found is to redundantly add types twice, which doesn't seem right: const GetByIdParams = Type.Object({
_id: Type.String(),
});
type GetByIdParams = Static<typeof GetByIdParams>;
// this first line is the workaround
fastify.get<{ Params: GetByIdParams }>(
"/:id",
{
schema: {
// ...
},
preHandler: [is_authenticated()],
},
async (request) => {
// same as before
}
); |
@baughmann, I might be able to assist you further if you can provide more complete repo to get wider context of the general setup, my generic steps from previous experience when trying to figure out why inference doesn't get resolved correctly is to
So my plugin definition will generally always looks like const plugin: FastifyPluginAsyncTypebox = async (fastify, _opts) => {
// fastify.get() here will be correctly typed
}
Recent example I went through, bumping my dependency from 0.29 -> 0.31 broke RegEx validation due to schema changes being discussed here. If you want less flaky update it might be beneficial to pin which TypeBox version to use. I hope this somewhat helps, this approach had helped me to avoid overall flaky-ness I had before. |
For people still facing this problem, this is the best solution to this problem that I could find for now:
A lot less clean than being able to just do:
I found similar unresolved issues on other type providers like zod, would really like to see a better solution for this issue. |
Please open new issues for new problems. This one was long solved, and adding comments here for seemingly unrelated things would not attract help. I'm locking this. |
Prerequisites
Fastify version
4.15.0
Plugin version
3.1.0
Node.js version
16.19.1
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
Monterey 12.6.5
Description
Hi,
Just updated plugin to version 3.1.0 recently and it looks like I lost types inference on the fastify routes handler request object. I rollbacked to 3.0.0 which is working fine. The following screenshot is showing that I no longer get types inference on the
params
object of the fastify request despite schema definition being provided and Typebox type provider being used.Thank you for your help ;)
Steps to Reproduce
Expected Behavior
req.params.foo
should be typed as astring
.strict
flag with typescript, I should not get the following error:The text was updated successfully, but these errors were encountered: