簡體   English   中英

如何在 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 錯誤:

解析錯誤:意外的令牌,應為“;”

在此處輸入圖像描述

這種方法也引發了錯誤: const form = <HTMLFormElement>document.getElementById('logout-form');

我在 a.tsx 文件和 a.ts 文件中都嘗試過同樣的 function 。

我究竟做錯了什么?

PS 我正在使用 Prettier 和 ESLint 以及https://github.com/airbnb/javascript規則和https://stackoverflow.com/a/64166241/470749

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

我認為主要的關鍵是將我的.eslintrc.js文件中的解析器更改為parser parser: '@typescript-eslint/parser', .

我還按照說明調整了pluginsextends

然后我將這些行添加到我的 .eslintrc.js 中的rules .eslintrc.js因為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'],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM