关于 TypeScript 的 noUnusedParameters 编译器选项的说明

2024-03-17

在 GitHub 上输入任何内容之前,我试图确定这实际上是否是一个错误。

With noUnusedParameters启用后,TypeScript 编译器会出现如下错误:

const foo = ['one', 'two', 'three'];
foo.forEach((item: string, index: number) => {
  // do something just with index, ignoring item
});

with error TS6133: 'item' is declared but never used.但是,虽然它没有被专门使用,但它被用于第二个参数forEach迭代器函数就是索引。

我错过了什么吗?


无需提出问题,因为问题已经存在:使用 --noUnusedParameters 如何跳过不需要的参数 https://github.com/Microsoft/TypeScript/issues/9458.

tl;dr:
您可以通过在无趣的参数前添加下划线来跳过此错误:

const foo = ['one', 'two', 'three'];
foo.forEach((_item: string, index: number) => {
    console.log(index);
});

编译良好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于 TypeScript 的 noUnusedParameters 编译器选项的说明 的相关文章

随机推荐