我正在使用Flow構建nodeJS應用程序,我需要擴展快速$ Request的默認快速註釋以適應其他字段,例如.user和.session。在流程中擴展快速請求類
不幸的是,當我試圖做到這一點,並創建接受這種新的請求類型的中間件時,Flow嚇了一跳,我不知道我做錯了什麼。
從流量類型的快遞原來的代碼是:
declare class express$Request extends http$IncomingMessage mixins express$RequestResponseBase {
....
}
declare type express$Middleware =
((req: express$Request, res: express$Response, next: express$NextFunction) => mixed) |
((error: ?Error, req: express$Request, res: express$Response, next: express$NextFunction) => mixed);
,所以我想我只是延長明示$請求,然後我所有的中間件應與新屬性的作用,對不對?
declare class web$Request extends express$Request {
user: any,
isAuthenticated(): boolean,
session: {
loginForwardUrl: ?string,
},
}
const authenticationMiddleware: express$Middleware = (
req: web$Request, res, next
): mixed => {
if (req.isAuthenticated()) {
return next();
}
req.session.loginForwardUrl = req.originalUrl;
return res.redirect('/auth/login/google');
}
不幸的是,這會產生超級複雜的錯誤:
function
This type is incompatible with
union: function type(s): web/src/index.js:113
Member 1:
function type: flow-typed/npm/express_v4.x.x.js:97
Error:
web$Request: web/src/index.js:114
This type is incompatible with the expected param type of
express$Request: flow-typed/npm/express_v4.x.x.js:97
Member 2:
function type: flow-typed/npm/express_v4.x.x.js:98
Error:
web$Request: web/src/index.js:114
This type is incompatible with an argument type of
null: flow-typed/npm/express_v4.x.x.js:98
誰能解釋一下是怎麼回事,如何解決?
謝謝!
錢幣,殘酷的 - 希望流將支持這更好的。謝謝! – user358829