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-support: error TS2339: Property 'urlData' does not exist on type 'FastifyRequest< #130

Closed
2 tasks done
honigkuchenbaer opened this issue Mar 26, 2023 · 5 comments

Comments

@honigkuchenbaer
Copy link

honigkuchenbaer commented Mar 26, 2023

Prerequisites

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

Issue

Writing code with types like

        handler:  async (req: FastifyRequest, reply: FastifyReply) => {
            const urlData = req.urlData()

won't compile with TypeScript because of the following error:

error TS2339: Property 'urlData' does not exist on type 'FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>

When I remove the type FastifyRequest, it works:

handler:  async (req, reply: FastifyReply) => {
    console.log(req.urlData)

I have seen in the source-code and the issues (#2 (comment)), that this plugin is enhancing the FastifyRequest-interface with the urlData-property.
However, I can not make it to work somehow.

What is the correct approach, to use TypeScript and make urlData available in FastifyRequest interface?

@mcollina
Copy link
Member

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.

@Uzlopak
Copy link
Contributor

Uzlopak commented May 14, 2023

I guess, because you would actually need to import the fastify-url-data package into your separate file, where you use FastifyRequest, so that typescript can hoist the additional typings.

@Eomm
Copy link
Member

Eomm commented Jul 19, 2023

Is this solved?

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 19, 2023

I think there is nothing to solve.

@Eomm Eomm closed this as completed Jul 20, 2023
@multiwebinc
Copy link

@Eomm This worked for me, an /app.ts:

import fastify from 'fastify';
import fastifyUrlData from '@fastify/url-data';

const server = fastify({
  // ... options
});

server.register(fastifyUrlData);

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

5 participants