简体   繁体   English

扩展 TypeScript 声明文件

[英]Extending TypeScript declaration files

I am trying to work with TypeScript and Express.我正在尝试使用 TypeScript 和 Express。 I've loaded in the type declarations from Typings and they look like this:我已经从 Typings 加载了类型声明,它们看起来像这样:

// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/express/express.d.ts
declare module "express" {
    import * as serveStatic from "serve-static";
    import * as core from "express-serve-static-core";

    /**
     * Creates an Express application. The express() function is a top-level function exported by the express module.
     */
    function e(): core.Express;

    namespace e {

        /**
         * This is the only built-in middleware function in Express. It serves static files and is based on serve-static.
         */
        var static: typeof serveStatic;

        export function Router(options?: any): core.Router;

        interface Application extends core.Application { }
        interface CookieOptions extends core.CookieOptions { }
        interface Errback extends core.Errback { }
        interface ErrorRequestHandler extends core.ErrorRequestHandler { }
        interface Express extends core.Express { }
        interface Handler extends core.Handler { }
        interface IRoute extends core.IRoute { }
        interface IRouter<T> extends core.IRouter<T> { }
        interface IRouterMatcher<T> extends core.IRouterMatcher<T> { }
        interface MediaType extends core.MediaType { }
        interface NextFunction extends core.NextFunction { }
        interface Request extends core.Request { }
        interface RequestHandler extends core.RequestHandler { }
        interface RequestParamHandler extends core.RequestParamHandler { }
        export interface Response extends core.Response { }
        interface Router extends core.Router { }
        interface Send extends core.Send { }
    }

    export = e;
}

Now I'm trying to extend the prototype for all Response objects like so:现在我正在尝试为所有 Response 对象扩展原型,如下所示:

const express = require('express');
express.response.sendWrapped = function(obj: Object, meta?: Object) {
    return this.json({
        data: obj
    });
};

Now I just need to get the extension into the typings.现在我只需要将扩展​​名添加到类型中。 I would love to just extend this method into the existing definition, much like how I have extended it in the prototype, but I am unsure how to do so.我很想将此方法扩展到现有定义中,就像我在原型中扩展它的方式一样,但我不确定如何这样做。

Since this is a personal extension of the type definition for the library, I do not believe this belongs back with the master typing set and I should not have to modify them manually for my own purposes.由于这是该库的类型定义的个人扩展,我相信这是属于回来的主打字集,我应该手动修改为我自己的目的。 What is the best way to extend them in my own way?以我自己的方式扩展它们的最佳方法是什么? Can I do it without overriding every other piece dependent on Response?我可以在不覆盖依赖于 Response 的所有其他部分的情况下做到这一点吗?

Using TypeScript 2.0 or a pre-release of it ( typescript@next ) you can use module augmentation syntax to augment the definition of express to include your new response definition:使用 TypeScript 2.0 或它的预发布版本 ( typescript@next ),您可以使用模块扩充语法来扩充express的定义以包含您的新response定义:

 import express = require('express'); declare module "express" { namespace response { function sendWrapped(obj: any, meta?: any): void; } } express.response.sendWrapped = function(obj: Object, meta?: Object) { return this.json({ data: obj }); };

A few things:一些东西:

(1) There's no response in the declarations you included, there's a Response , so it should be: express.Response.sendWrapped . (1)您包含的声明中没有response ,有一个Response ,所以它应该是: express.Response.sendWrapped

(2) You did not extend the prototype for all Response s, you added the sendWrapped function to the class itself (I assume that Response is a class). (2) 您没有为所有Response扩展原型,而是将sendWrapped函数添加到类本身(我假设Response是一个类)。
If you want to add to the prototype it should look like:如果要添加到原型中,它应该如下所示:

express.Response.prototype.sendWrapped = function(obj: Object, meta?: Object) {
    return this.json({
        data: obj
    });
};

(3) You can add to an existing type like so: (3) 您可以像这样添加到现有类型:

namespace express {
    interface Response {
        sendWrapped(obj: Object, meta?: Object): any;
    }
}

More about that in Declaration Merging更多关于声明合并的信息


Edit编辑

If it's in a d.ts file then you need it to be:如果它在d.ts文件中,那么你需要它是:

declare namespace express {
    ...
}

I'm not sure why it doesn't let you merge declarations and I don't have this setup to test it on my own, but I have another solution for you which is, in my opinion, better.我不确定为什么它不允许您合并声明,而且我没有这个设置来自己测试它,但我有另一个解决方案供您使用,在我看来,它更好。

Just extend express.Response add what ever you need in there, for example:只需扩展express.Response添加您需要的内容,例如:

class MyResponse extends express.Response {
    constructor() {
        super();
    }

    sendWrapped(obj: Object, meta?: Object): express.Response {
        return this.json({
            data: obj
        });
    }
}

This is better because you're not as vulnerable for changes in the express api, and more than that, this is what object oriented is for.这更好,因为您不容易受到 express api 更改的影响,而且更重要的是,这就是面向对象的用途。

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

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