在 TypeScript 中 document.getElementById() 方法的类型转换期间如何处理 ESLint 错误?

How to handle ESLint error during type casting of document.getElementById() method in TypeScript?

根据所有这些页面:

我预计这会起作用:

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

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

Parsing error: Unexpected token, expected ";"

这种方法也引发了错误:const form = <HTMLFormElement>document.getElementById('logout-form');

我已经在 .tsx 文件和 .ts 文件中尝试过相同的功能。

我做错了什么?

P.S。我正在使用 Prettier 和 ESLint 以及 https://github.com/airbnb/javascript rules and

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

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

我也按照说明调整了pluginsextends

然后我将这些行添加到 .eslintrc.js 中的 rules 对象,因为 :

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