我正在尝试使用声明合并来扩展expressjs Application接口,如明确类型定义
中所述declare module Express {
// These open interfaces may be extended in an application-specific manner via declaration merging.
// See for example method-override.d.ts (https://github.com/borisyankov/DefinitelyTyped/blob/master/method-override/method-override.d.ts)
export interface Request { }
export interface Response { }
export interface Application { }
}
所以,我的app.ts看起来像这样:
/// <reference path="typings/express/express.d.ts" />
declare module Express {
export interface Application {
testA: string;
}
export interface Request {
testR: string;
}
}
import express = require('express');
var app = express();
app.testA = "why not?";
app.use(function (req, res, next) {
req.testR = "xxx";
})
我收到了错误:
&#34;属性testA在Express&#34;
类型上不存在&#34;属性testR在Request&#34;
类型上不存在任何线索?
答案 0 :(得分:2)
由于您使用的是模块,因此此处不会发生声明合并。在app.ts
中没有要合并的Express
模块,所以它正在制作一个完全独立的模块定义。你需要移动代码......
declare module Express {
export interface Application {
testA: string;
}
export interface Request {
testR: string;
}
}
...进入.d.ts
文件,以便接口与express.d.ts
中的接口合并。