[英]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.