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
TypeScript issue when passing Pino instance to fastify constructor #4960
Comments
Thanks for reporting! Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that. |
We have the same issue but its because we are assigning the fastify instance to a variable with type FastifyInstance. Since we don't specify the generics the logger type defaults to FastifyBaseLogger, which doesn't seem to be compatible with any pino instance. Seems like the new FastifyBaseLogger type is not correct. |
Yeah, we also assign the server to a variable of type |
Why should it be related #4928? |
@ziimakc I've literally tested your example with the top-most version of Fastify, Pino and TypeScript and I could not reproduce. Please provide a full repro including the |
@mcollina here it is: https://github.com/ZiiMakc/fastify-type-errr run:
|
This is confirmed only with There seem to be an issue with:
Which I think it's due to: Line 24 in c235d2d
not being compatible with https://github.com/pinojs/pino/blob/91a3505cfaef323f565e3cfe93f5d5077320faff/pino.d.ts#L83 I don't know why this problem shows up only in strict mode and how to change this. At a first glance these seems very similar. However some of those generics are slightly different. Anyhow, this fixes your problem: import { fastify, type FastifyInstance, type FastifyBaseLogger } from "fastify";
import { pino } from "pino";
// all latest versions
const log = pino();
const app: FastifyInstance = fastify({
logger: log as FastifyBaseLogger,
}); |
We faced the same problem, and the only solution is to use the |
In the const log = pino();
const app: FastifyInstance = fastify({
logger: log,
}); case, we are assigning And the And function parameters in TypeScript are contravariant when https://www.typescriptlang.org/docs/handbook/type-compatibility.html#comparing-two-functions This should also work, const log = pino();
const app = fastify({
logger: log,
}); |
How would you fix it? |
@flakey5 could you take a look? |
From my testing I believe I can second what @NotEvenANeko said about this. It's not so much const log = pino();
const a: FastifyBaseLogger = log;
const b: boolean | FastifyLoggerOptions<RawServerDefault> & PinoLoggerOptions | Logger = log; // type of the `logger` property in `FastifyOptions` but instead It does appear to be connected to #4760, removing Few things we can do to fix it (starting with good ending with bad),
|
@flakey5 would you like to send a PR for option 1? |
Prerequisites
Fastify version
4.21.0
Plugin version
No response
Node.js version
18.14.2
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.5
Description
By some historical reasons we pass a Pino instance to fastify factory function (I'm aware that Pino is built-in into fastify):
But starting
v4.20.0
this code is no longer working with the following TypeScript compile error:It seems to be related to this recent change - #4760
Steps to Reproduce
fastify
factory functionExpected Behavior
No response
The text was updated successfully, but these errors were encountered: