我正在使用fastify和插件fastify-static。我还为这个插件提供了我自己的 TypeScript 类型声明typings/fastify-static/index.d.ts
:
declare module "fastify-static" {
import { Plugin } from "fastify";
import { Server, IncomingMessage, ServerResponse } from "http";
namespace fastifyStatic {
const instance: Plugin<Server, IncomingMessage, ServerResponse, any>;
}
export = fastifyStatic.instance
}
FastifyReply
另外插件用 method扩展了 fastify sendFile
。
当我像这样在模块范围内增加 fastify 模块时,工作正常:
// server.js
import fastify from "fastify";
import fastifyStatic from "fastify-static";
declare module "fastify" {
interface FastifyReply<HttpResponse> {
sendFile: (file: string) => FastifyReply<HttpResponse>
}
}
server.get("/file", async (request, reply) => {
reply.sendFile('file')
});
不幸的是,它只适用于这个模块。当我将声明移至typings/fastify-static/index.d.ts
或typings/fastify/index.d.ts
覆盖模块而不是扩充时。
如何fastify
在项目范围内扩充模块?