Dea*_*ady 6 interface typescript
在TypeScript中扩展Express.Request接口时遇到了我想要使用外部库定义的问题,但我无法导入外部库,因为它会导致错误 - >
错误:(4,28)TS1147:内部模块中的导入声明无法引用外部模块.
编辑:这是一个.d.ts文件
/// <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;
}
}
Run Code Online (Sandbox Code Playgroud)
试图引用bunyan.d.ts(https://github.com/borisyankov/DefinitelyTyped/blob/master/bunyan/bunyan.d.ts)也导致问题,因为bunyan模块导出为字符串
declare module "bunyan" {
...
}
Run Code Online (Sandbox Code Playgroud)
因此尝试使用它从参考结果中找不到.
/// <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'.
}
}
Run Code Online (Sandbox Code Playgroud)
TL;博士; 如何使用外部模块定义扩展接口定义.
内部和外部模块的引用将在 v1.5 中得到改进,目前处于 alpha 版本 ( http://blogs.msdn.com/b/typescript/archive/2015/03/27/announcing-typescript-1- 5-alpha.aspx)。
同时,您可以bunyan通过以下方式导入您的模块:
var bunyan = require('bunyan');
Run Code Online (Sandbox Code Playgroud)