Skip to content

Example of decorateRequest can't pass type checking #4870

@trim21

Description

@trim21

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

https://fastify.dev/docs/latest/Reference/Decorators/#decoratereplyname-value-dependencies

import { fastify } from 'fastify';


declare module 'fastify' {
  interface FastifyRequest {
    foo: Readonly<{ bar: number }>;
  }
}

const app = fastify({});
// @ts-expect-error
app.decorateRequest('foo', null); <- `null` doens't satisfy type `{bar: number}`
app.addHook('onRequest', async (req, reply) => {
  req.foo = { bar: 42 };
});

I don't think change type of .foo to ... | null is a good idea, so the only possible solution I can come up with is to use @ts-ignore or @ts-expect-error on line decorateRequest.

caused by #4858

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions