DEV Community

Ethan Arrowood
Ethan Arrowood

Posted on

TypeScript support for Pino with Fastify

By default, Fastify ships with a Pino instance as its logger; however, since Pino types are not maintained (yet) by the Pino project itself, Fastify does not ship type support for the complete Pino API. Understandably, this can be frustrating, but in this post I want to show you just how easy it is to add @types/pino to your Fastify app.

  1. Install Pino Types

    npm i -D @types/pino
    
  2. Augment the FastifyLoggerInstance type in the same file the app is instantiated in (generally index.ts or server.ts)

    import type { Logger } from 'pino'
    declare module 'fastify' {
        interface FastifyLoggerInstance extends Logger {}
    }
    

This works because in the v3 Fastify types, the FastifyInstance.log property is defined as a generic Logger that is defaulted to FastifyLoggerInstance (ref). Declaration merging the FastifyLoggerInstance type with interface FastifyLoggerInstance extends Logger {}, informs TypeScript to consider the type as an extension of the logger type defined in @types/pino.

Top comments (0)