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

TypeScript doesn't resolve the correct type of the logger inside controller (req.log) #347

Open
svex99 opened this issue Aug 15, 2024 · 0 comments

Comments

@svex99
Copy link

svex99 commented Aug 15, 2024

Problem

I have a logger with a custom level that is used as base to create the logger passed to Express.

import pino from 'pino';
import pinoHTTP from "pino-http";

export const logger = pino<"newLevel">({
    customLevels: {
        newLevel: 100,
    },
})

const httpLogger = pinoHTTP({
    logger: logger,
});

export default httpLogger;

However when I access the log property from the request it doesn't point to the right type of the logger being used (Logger<"newLevel">), instead it points to Logger<never> (from the base pino.d.ts AFAIK).

This causes TypeScript to complain when called the method req.log.newLevel(...) inside a controller.

app.get('/test', (req, res) => {
    req.log.trace('trace log') // TypeScript doesn't complain!
    req.log.newLevel('newLevel log')
    //      \__ Property 'newLevel' does not exist on type 'Logger<never>'.ts(2339)
    res.status(200)
})

Question

How could I point TypeScript to look at the correct type of the logger inside the controllers when using custom levels?

Thanks in advance!

@svex99 svex99 changed the title req.log type doesn't resolve the correct type of the logger TypeScript doesn't resolve the correct type of the logger inside controller (req.log) Aug 15, 2024
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

1 participant