ZiiMakc
ZiiMakc

Reputation: 36806

Typescript error - "type is declared but never used" in .ts. When it was used in interface

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>

Upvotes: 0

Views: 748

Answers (1)

Plochie
Plochie

Reputation: 4117

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
}

Upvotes: 1

Related Questions