0

Why typescript give me error "'HttpRequest' is declared but never used." when HttpRequest was used in interface? How to fix this?

import fp from 'fastify-plugin'
import fastify from 'fastify'
import { IncomingMessage } from 'http'
import { Http2ServerRequest } from 'http2'

//'HttpRequest' is declared but never used.
type HttpRequest = IncomingMessage | Http2ServerRequest

declare module 'fastify' {
  interface FastifyRequest<
    HttpRequest,
    Query = fastify.DefaultQuery,
    Params = fastify.DefaultParams,
    Headers = fastify.DefaultHeaders,
    Body = any
  > {
    user: string
  }
}

function plugin(fastify, options, next) {
  fastify.decorateRequest('user', 'userData')
  next()
}

export const reqDecorator = fp(plugin)

Here is the FastifyRequest interface:

interface FastifyRequest<HttpRequest = IncomingMessage, Query = fastify.DefaultQuery, Params = fastify.DefaultParams, Headers = fastify.DefaultHeaders, Body = any>
2
  • is this .ts file or .d.ts file? Commented Nov 22, 2019 at 12:34
  • it's .ts file....... Commented Nov 22, 2019 at 12:46

1 Answer 1

1

You have not defined name for the type,

declare module 'fastify' {
  interface FastifyRequest<
    httpRequest = HttpRequest,

As IncomingMessage is used as type, again you need to give name for the type for FastifyRequest interface,

export interface FastifyRequest<httprequest = IncomingMessage, // added this
Query = fastify.DefaultQuery,
Params = fastify.DefaultParams,
Headers = fastify.DefaultHeaders,
Body = any> {
  // interface fields and methods
}
Sign up to request clarification or add additional context in comments.

3 Comments

Added FastifyRequest interface. If i do like HttpRequest = HttpRequest i've get "All declarations of 'FastifyRequest' must have identical type parameters"
but FastifyRequest is part of fastify framework, i can't modify it and export ;/ Or i just don't understand something
httprequest = IncomingMessage gives the same error: "All declarations of 'FastifyRequest' must have identical type parameters"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.