Skip to content

TypeScript error: Property 'url' does not exist on type 'FastifyContextConfig'. #4788

@pkyeck

Description

@pkyeck

Prerequisites

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

Fastify version

4.17.0

Plugin version

No response

Node.js version

18

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.3.1

Description

I'm using TypeScript and when I try to access the request's routeConfig.url TS complains about url not existing.

Looking at the types for FastifyContextConfig finds an empty interface ... is this correct? Do I have to set this manually although the url and method are built-in attributes?

Steps to Reproduce

import Fastify, {
  FastifyRequest,
  FastifySchema,
  FastifyTypeProviderDefault,
  RawRequestDefaultExpression,
  RawServerDefault,
  RouteGenericInterface,
} from "fastify";

const fastify = Fastify({
  logger: false,
});

// TS is complaining about unknown url
fastify.addHook("onRequest", async function onRequest(req) {
  console.log(req.routeConfig.url);
});

// this works :( but I don't want to be the one telling fastify about their built-in attributes
fastify.addHook(
  "onRequest",
  async function onRequest(
    req: FastifyRequest<
      RouteGenericInterface,
      RawServerDefault,
      RawRequestDefaultExpression<RawServerDefault>,
      FastifySchema,
      FastifyTypeProviderDefault,
      { url: string }
    >
  ) {
    console.log(req.routeConfig.url);
  }
);

like to codesandbox: https://codesandbox.io/p/sandbox/fastify-routeconfig-url-xndp24

Expected Behavior

// in this case req.routeConfig should know about the url attribute 
fastify.addHook("onRequest", async function onRequest(req) {
  console.log(req.routeConfig.url);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersmissing typesPR that does not implement TypeScript changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions