扩展快速应用界面
我正在尝试使用声明合并来扩展expressjs应用程序接口,如快速types定义中所述
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"; })
我得到的错误:
“Expresstypes上不存在属性testA”
“请求types中不存在属性testR”
任何线索?
由于您正在使用模块,所以在这里不会发生声明合并。 在app.ts
,没有一个Express
模块可以合并,所以它是一个完全独立的模块定义。 你需要移动代码…
declare module Express { export interface Application { testA: string; } export interface Request { testR: string; } }
…转换为.d.ts
文件,以便将接口与express.d.ts
中的接口合并。