0

我正在使用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.tstypings/fastify/index.d.ts覆盖模块而不是扩充时。 如何fastify在项目范围内扩充模块?

4

1 回答 1

0

提香·切尔尼科娃-德拉戈米尔是对的。模块扩充喊在 中typings/fastify-static/index.d.ts,但不是作为单独的模块声明。

// 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

    module "fastify" {
        interface FastifyReply<HttpResponse> {
            sendFile: (file: string) => FastifyReply<HttpResponse>
        }
    }
}
于 2018-09-26T12:27:32.730 回答