【问题标题】:How to handle ESLint error during type casting of document.getElementById() method in TypeScript?如何在 TypeScript 中 document.getElementById() 方法的类型转换期间处理 ESLint 错误?
【发布时间】:2021-08-03 22:27:07
【问题描述】:

根据所有这些页面:

我希望这会起作用:

/**
 * Destroy current user session.
 */
function logOut() {
  const form = document.getElementById('logout-form') as HTMLFormElement;
  form.submit();
}

相反,我在 VS Code 中看到了这个 ESLint 错误:

解析错误:意外的令牌,应为“;”

这种方法也会出错:const form = <HTMLFormElement>document.getElementById('logout-form');

我在 .tsx 文件和 .ts 文件中都尝试过同样的功能。

我做错了什么?

附:我正在使用 Prettier 和 ESLint 以及 https://github.com/airbnb/javascript 规则和 https://stackoverflow.com/a/64166241/470749

【问题讨论】:

标签: typescript eslint eslintrc eslint-config-airbnb


【解决方案1】:

https://khalilstemmler.com/blogs/typescript/eslint-for-typescript/#Installation-and-setup 非常有帮助。

我认为主要的关键是将我的.eslintrc.js 文件中的parser 更改为parser: '@typescript-eslint/parser',

我还按照指示调整了pluginsextends

然后我将这些行添加到我的.eslintrc.js 中的rules 对象中,因为https://stackoverflow.com/a/64024916/

// We must disable the base rule (since it can report incorrect errors) and replace it (https://stackoverflow.com/a/64024916/):
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': ['error'],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 2020-10-21
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多