Typescript Interface Extension

When expanding the Express.Request interface in TypeScript, I came across this problem that I want to use the definition of an external library, but I can not import the external library, as this leads to an error →

Error: (4, 28) TS1147: import declarations in the internal module cannot refer to the external module.

Edit: this 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 link to bunyan.d.ts ( https://github.com/borisyankov/DefinitelyTyped/blob/master/bunyan/bunyan.d.ts ) There is also a problem because the bunyan module is exported as a string

declare module "bunyan" {
...
}

As such, trying to use it from referenced 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; dg; How to extend the definition of an interface using the definition of an external module.

+4
2

, , require, extends.

, :

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

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

, .

+2
+1
source

All Articles