TypeScript ESLint错误地报告:";函数已定义但从未使用过;

TypeScript ESLint错误地报告:";函数已定义但从未使用过;,typescript,eslint,typescript-eslint,Typescript,Eslint,Typescript Eslint,我在lib\iw browser.ts文件中有iwaddclassandremoveinslings函数: "use strict"; /* Adds given CSS class to given element and remove this class in element's siblings. Equal to jQuery: $(element1).addClass(CSSClass).siblings().removeClass(CSSClass) */ function

我在
lib\iw browser.ts
文件中有
iwaddclassandremoveinslings
函数:

"use strict";

/* Adds given CSS class to given element and remove this class in element's siblings.
   Equal to jQuery: $(element1).addClass(CSSClass).siblings().removeClass(CSSClass) */
function iwAddClassAndRemoveInSiblings(element: Element, CSSClass: string): void {
  for (const sibling of element.parentNode.children)
    sibling.classList.remove(CSSClass)
  element.classList.add(CSSClass)
}
document.addEventListener('click', (event) => {
  const target = <HTMLElement>event.target
  if (target.matches('.iw-carousel__indicators li'))
    iwCarouselShowSlide(target.closest('.iw-carousel'), Number(target.dataset.slideTo))
})

/* Shows i-th slide of the given iw-carousel. */
const iwCarouselShowSlide = (carousel: HTMLElement, slideIndex: number) => {
  const slides = carousel.querySelectorAll('.iw-carousel__item')
  iwAddClassAndRemoveInSiblings(slides[slideIndex], 'active')
}
我在
lib\iw carousel\iw carousel.ts
文件中调用此函数:

"use strict";

/* Adds given CSS class to given element and remove this class in element's siblings.
   Equal to jQuery: $(element1).addClass(CSSClass).siblings().removeClass(CSSClass) */
function iwAddClassAndRemoveInSiblings(element: Element, CSSClass: string): void {
  for (const sibling of element.parentNode.children)
    sibling.classList.remove(CSSClass)
  element.classList.add(CSSClass)
}
document.addEventListener('click', (event) => {
  const target = <HTMLElement>event.target
  if (target.matches('.iw-carousel__indicators li'))
    iwCarouselShowSlide(target.closest('.iw-carousel'), Number(target.dataset.slideTo))
})

/* Shows i-th slide of the given iw-carousel. */
const iwCarouselShowSlide = (carousel: HTMLElement, slideIndex: number) => {
  const slides = carousel.querySelectorAll('.iw-carousel__item')
  iwAddClassAndRemoveInSiblings(slides[slideIndex], 'active')
}
不幸的是,Typescript ESLint间接地将
iwaddclassandremoveinsplings
函数报告为未使用,这两个函数都是在Visual Studio代码中,如果从命令行
npx ESLint运行的话--分机ts

'iwAddClassAndRemoveInSiblings' is defined but never used. eslint(@typescript-eslint/no-unused-vars)

HTML页面显示正确-它运行
iwAddClassAndRemoveInSiblings
功能没有问题。Visual Studio代码还知道使用了iwaddclassandremoveinsplings函数。如果我尝试使用一个不存在的函数,VSC会说:找不到名称“nonexistingfunction”。因此VSC检查函数是否已定义。我只有ESLint有这个问题

我是否错误地配置了ESLint或Typescript

我已按照以下所述方式安装了TypeScript和ESLint:

Typescript和ESLint配置文件如下所示:

.eslintrc.js:

module.exports={
根:是的,
解析器:'@typescript eslint/parser',//允许eslint理解typescript语法
解析选项:{
项目:['./tsconfig.json'],//对于“类型感知林廷”是必需的
},
插件:[
“@typescript eslint”,//允许在代码库中使用规则
],
扩展:[
'airbnb typescript/base',//使用airbnb配置
],
规则:{}
};
tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2016",
    "module": "commonjs",
    "sourceMap": true
  }
}
package.json:

{
  "name": "iw-components",
  "version": "0.1.0",
  "description": "...",
  "main": "index.js",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/iwis/iw-components.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/iwis/iw-components/issues"
  },
  "homepage": "https://github.com/iwis/iw-components#readme",
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^2.34.0",
    "@typescript-eslint/parser": "^2.34.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-typescript": "^7.2.1",
    "eslint-config-standard-with-typescript": "^16.0.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1",
    "typescript": "~3.7.5"
  }
}

问题是ESLint不知道它是一个全局函数。您有两个选择(至少):

  • 不要使用globals-globals在历史上是很多bug的来源。改用模块。或者
  • 告诉ESLint这是一个全球性的问题
  • #2有两个部分:在定义函数的文件中,您需要告诉ESLint函数不是“未使用的”,在使用它的文件中,您需要告诉ESLint关于它的情况

    有关详细信息,请参见文档,但基本上:

    • 要告诉ESLint函数未使用,请使用注释,例如:

    • 要告诉ESLint它存在(在另一个文件中),请使用注释,例如:



    但我强烈建议改用模块。

    我认为我的问题中包含了所有内容。为了以防万一,我添加了代码库的链接。你在评论中提到了它,所以我现在可以从我的问题中删除它。正如我在上面的评论中所说,请添加代码,显示上面的
    iwaddclassandremoveinsplings
    的导出和导入。它不在那里——至少不是作为文本;如果它隐藏在图像中,那是另一个问题:请始终将代码、错误消息、标记等作为文本发布,而不是作为文本的图片发布。为什么:(一张图片可能是一个很好的补充。)完成。我希望现在包括所有相关代码。我不导出和导入函数。到目前为止,它还没有出现任何问题。只有ESLint开始报告一个问题。啊!这就是问题所在。你需要告诉ESLint这是一个全球性的。
    /* global iwAddClassAndRemoveInSiblings */