【问题标题】:How do I use express.Router in ts and eslint without error如何在 ts 和 eslint 中使用 express.Router 而不会出错
【发布时间】:2021-09-24 00:40:44
【问题描述】:

我已尝试导出如下所示的控制器

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 但这不可行,我不想每次要导出控制器时都粘贴它。

【问题讨论】:

    标签: typescript eslint typescript-eslint


    【解决方案1】:

    通过添加此评论禁用:

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

    【讨论】:

    • 最好放入 .eslint 配置文件,如其他答案所示??
    【解决方案2】:

    您可以使用capIsNewExceptions option which:

    允许在没有 new 运算符的情况下调用指定的以大写开头的函数名。

    将其放入您的 ESLint 配置中:

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

    【讨论】:

    • 'new-cap': ['error', { capIsNewExceptions: 'Router' }], 如果在.eslint 配置文件中,那么不会以"eslint 开头,对吧?而且,当我这样做时似乎对我不起作用:['Router'](在[])。
    猜你喜欢
    • 2022-01-11
    • 2020-07-01
    • 2019-10-26
    • 2017-01-31
    • 1970-01-01
    • 2021-02-11
    • 2021-01-27
    • 2021-08-25
    • 2020-11-22
    相关资源
    最近更新 更多