简体   繁体   English

当映射函数打字稿中未使用索引时,摆脱 eslint@typescript-eslint/no-unused-vars 错误

[英]Get rid of eslint@typescript-eslint/no-unused-vars error when index is not used in map function typescript

I got this code:我得到了这个代码:

const reducer = (element:number, index: number) => [element]; //es lint error.
const positionsArray = $.map(this.positions, reducer);

I am casting a Float32Array (it is this.positions) to a js array.我正在将一个 Float32Array(它是 this.positions)转换为一个 js 数组。 I get the error: 'index' is defined but never used.eslint@typescript-eslint/no-unused-vars我收到错误:'index' 已定义但从未使用过。eslint@typescript-eslint/no-unused-vars

I cannot apply the "// eslint-disable-line no-unused-vars" (which btw I dont think is a pleasant solution)我不能应用“// eslint-disable-line no-unused-vars”(顺便说一句,我认为这不是一个令人愉快的解决方案)

How to get rid of this error for this case that I don't need the index?在我不需要索引的情况下,如何摆脱这个错误? I checked reduce and try to retrieve the accumulator but did not achieve it.我检查了 reduce 并尝试检索累加器,但没有实现。 Thanks.谢谢。

EDIT: If I dont define the index I get this error:编辑:如果我不定义索引,我会收到此错误: 在此处输入图片说明

Don't define the parameter.不要定义参数。 Neither TypeScript nor JavaScript requires you to use all parameters that a callback might accept. TypeScript 和 JavaScript 都不要求您使用回调可能接受的所有参数。

You can also convert the typed array to a normal array by invoking the iterator - it looks like jQuery's .map doesn't behave well with it, so use plain JS instead:您还可以通过调用迭代器将类型化数组转换为普通数组 - 看起来 jQuery 的.map与它表现不佳,因此请改用普通 JS:

const positionsArray = [...this.positions].map(element => [element]);

If you just want to get a plain array from the typed array, don't use .map , just invoke the iterator alone:如果您只想从类型化数组中获取一个普通数组,请不要使用.map ,只需单独调用迭代器:

const positionsArray = [...this.positions];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM