繁体   English   中英

Typescript 错误 - “类型已声明但从未使用” in.ts。 在界面中使用时

[英]Typescript error - “type is declared but never used” in .ts. When it was used in interface

为什么 typescript 给我错误“'HttpRequest' 已声明但从未使用。” 什么时候在接口中使用了HttpRequest? 如何解决这个问题?

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)

这里是 FastifyRequest 接口:

interface FastifyRequest<HttpRequest = IncomingMessage, Query = fastify.DefaultQuery, Params = fastify.DefaultParams, Headers = fastify.DefaultHeaders, Body = any>

您尚未定义类型的名称,

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

由于IncomingMessage被用作类型,你需要再次为FastifyRequest接口的类型命名,

export interface FastifyRequest<httprequest = IncomingMessage, // added this
Query = fastify.DefaultQuery,
Params = fastify.DefaultParams,
Headers = fastify.DefaultHeaders,
Body = any> {
  // interface fields and methods
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM