简体   繁体   English

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

[英]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."为什么 typescript 给我错误“'HttpRequest' 已声明但从未使用。” when HttpRequest was used in interface?什么时候在接口中使用了HttpRequest? 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:这里是 FastifyRequest 接口:

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

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,由于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.

相关问题 TypeScript:构造函数属性已声明但从未使用 - TypeScript: Constructor Property is declared but never used 忽略 TS6133:“(导入)已声明但从未使用过”? - Ignore TS6133: "(import) is declared but never used"? Typescript 错误 TS2449: Class 'x' 在其声明之前使用,并确保在此处声明 'X' - Typescript error TS2449: Class 'x' used before its declaration, and insure that 'X' is declared here 在typescript中的type参数中使用的never关键字 - never keyword used in type parameter in typescript 与通用接口一起使用时,Typescript 无法正确推断类型 - Typescript is not infering type correctly when used with generic interface 属性&#39;平台&#39;已声明但从未使用过 - Property 'platform' is declared but never used TS 索引类型 - 在接口中使用索引类型时,编译器无法解析确切值 - TS index types - compiler cannot resolve exact value when index type is used in interface 如何修复 eslint 错误,即在实际使用 TypeScript props 接口时未使用它? - How can I fix the eslint error saying a TypeScript props interface is not used when it's actually used? Typescript:错误 TS2693:“注意”仅指一种类型,但在此处用作值 - Typescript: error TS2693: 'Note' only refers to a type, but is being used as a value here TS2749:'Schema' 指的是一个值,但在定义 Mongoose model 和 TypeScript 时在这里用作类型 - TS2749: 'Schema' refers to a value, but is being used as a type here when defining Mongoose model with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM