有沒有辦法來 「覆蓋」 時做declartion合併,例如:Typescript聲明合併:重寫?
app.ts(+表達的NodeJS):
import * as express from 'express';
var app = express();
app.use(function(req, res, next) {
console.log(req.headers.accept);
});
這種失敗,錯誤:
error TS2339: Property 'accept' does not exist on type '{ [key: string]: string; }'.
因爲在express.d.ts標頭聲明如下:
headers: { [key: string]: string; };
那麼什麼我一直試圖做的,就是創造了「接受」的定義:
declare module Express {
export interface Headers {
accept: String
}
export interface Request {
headers: Headers
}
}
但是,這並不工作,也(我只能添加新成員,不能覆蓋他們,對不對?):
error TS2430: Interface 'e.Request' incorrectly extends interface 'Express.Request'.
Types of property 'headers' are incompatible.
Type '{ [key: string]: string; }' is not assignable to type 'Headers'.
Property 'accept' is missing in type '{ [key: string]: string; }'.
因此,要克服這一點的唯一方法是改變符號來:
req.headers.accept -> req.headers['accept']
或者不知何故,我可以「重新聲明」的標題屬性?
您使用哪個「express.d.ts」文件? –
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/express/express.d.ts – gerasalus