【发布时间】:2017-10-23 01:17:57
【问题描述】:
使用 typescript 和 nodejs,以及 express-jwt 来验证真实性。 当我们将 express-jwt 与 unless 一起使用时,如下所示,tslint 抱怨在 unless 函数之后可能返回的未定义值。
import * as express from "express";
import * as jwt from "express-jwt";
import * as http from "http";
const refConfig = {hash: 'mycompletelysecrethashstring', othersetting: 1 };
app.use("/api", jwt({ secret: refConfig.hash }).unless({ path: ["/api/auth/signin"] }));
app.set("port", 8000);
const server: http.Server = app.listen(app.get("port"), () => {
console.log("Server is now running on port " + server.address().port);
});
错误信息如下:
[ts] Object is possibly 'undefined'.
是否有可能摆脱此警告,如果可以,正确的做法是什么?
问题在于以下行:
app.use("/api", jwt({ secret: refConfig.hash }).unless({ path: ["/api/auth/signin"] }));
据我所知,TypeScript 编译器抱怨路由列表在除非结尾可能是一个空列表。鉴于原始列表是“/api/auth/signin”的超集,这不是一个有效的问题,但我无法摆脱警告。
提前致谢。
【问题讨论】:
标签: node.js typescript visual-studio-code express-jwt