Extending typescript interface

此生再无相见时 提交于 2019-12-22 05:23:25

问题


When extending the Express.Request interface in TypeScript I ran into this problem that I want to use an external library definition, but I can't import the external library as it results in error ->

Error:(4, 28) TS1147: Import declarations in an internal module cannot reference an external module.

Edit: It is a .d.ts file

/// <reference path="../typings/express/express.d.ts" />

declare module Express {
    import bunyan = require('bunyan'); <-- results in error
    export interface Request {
        _id: string; <-- this works
        log: bunyan.Logger; <-- Here I want to define that it is bunyan.Logger instance;
    }
}

Trying to reference the bunyan.d.ts (https://github.com/borisyankov/DefinitelyTyped/blob/master/bunyan/bunyan.d.ts) Also results in a problem, as the bunyan module is exported as string

declare module "bunyan" {
...
}

As such trying to use it from reference results in not found.

/// <reference path="../typings/express/express.d.ts" />
/// <reference path="../typings/bunyan/bunyan.d.ts" />

declare module Express {
    export interface Request {
        _id: string;
        log: bunyan.Logger; <- Error:(8, 18) TS2304: Cannot find name 'bunyan'.
    }
}

tl;dr; How to extend interface definition with external module definitions.


回答1:


I don't think you can add to an existing interface when a require is necessary, but you can extend the existing interface using the extends keyword.

Move your import statement outside your module, export your module, and extend the existing interface:

import bunyan = require('bunyan');
import express = require('express');

export declare module ExtendedExpress {
    export interface Request extends express.Express.Request {
        _id: string;
        log: bunyan.Logger;
    }
}

Then you have to import this module where you want to use it.




回答2:


The referencing of internal and external modules will be improved in v1.5 which is currently in an alpha release (http://blogs.msdn.com/b/typescript/archive/2015/03/27/announcing-typescript-1-5-alpha.aspx).

In the meantime you can import in your bunyan module via:

var bunyan = require('bunyan');


来源:https://stackoverflow.com/questions/29516522/extending-typescript-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!