如何在 ts 和 eslint 中使用 express.Router 不报错

How do I use express.Router in ts and eslint without error

我试过像下面这样导出控制器

export const IndexController: Router = Router();

我收到以下错误:

A function with a name starting with an uppercase letter should only be used as a constructor.eslintnew-cap

我尝试用 'new' 运算符作为开头并得到以下错误。

'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

我知道的唯一解决方案是添加注释 eslint-disable-next-line new-cap 但这不可行,我不想每次导出控制器时都粘贴它。

您可以使用 capIsNewExceptions option which:

allows specified uppercase-started function names to be called without the new operator.

将其放入您的 ESLint 配置中:

"eslint new-cap": ["error", { "capIsNewExceptions": ["IndexController"] }]

通过添加此评论禁用:

/* eslint new-cap: ["error", { "capIsNewExceptions": ["Router"] }] */
const routes = express.Router()