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.
declareindeclare module Express?requiremisled me, I believed it was a.tsfile..d.tsexists for the external modulebunyan, you can reference it with/// <reference ...and then use it to typeRequest.log.Loggerinstead ofbunyan.Logger. If it still not works, then you can try to upgrade to typescript 1.5-alpha and to use the external module import syntax. Or edit the.d.tsfile and remove the quotes indeclare module "bunyan"=>declare module bunyan. Without quotes,bunyanbecomes a namespace andbunyan.Loggerbecomes available.