
[英]react, typescript Argument of type 'number' is not assignable to parameter of type 'ChangeEvent<HTMLInputElement>'
[英]Typescript Error: Argument of type 'NodeListOf<HTMLInputElement> | undefined' is not assignable to parameter of type 'Iterable<HTMLInputElement> ..."
在我的 React/Typescript 应用程序中,我通过使用“e.currentTarget”访问事件侦听器中的输入数组。 我正在使用 Array.from() 将 NodeListOf 转换为数组,并确保配置我的 TS.config.json 设置为“es2015”。 但是我收到了这个错误。
错误
奇怪的是,这个错误不会发生在我的原始目录中。 我已经复制了我的项目文件夹,删除了节点模块,将其压缩并重新打开,这就是发生此错误的时候。 不太确定如何解决此问题,如果有人有建议或解决方案,将不胜感激。
e: React.ChangeEvent<HTMLInputElement> | React.KeyboardEvent<Element>,
letter: string,
idx: number,
) => {
const fields: HTMLInputElement[] = Array.from(
e.currentTarget.parentElement?.parentElement?.querySelectorAll('input')
)
const length: number = fields.length
const position: number = fields.indexOf(e.target)
您没有提供太多代码,但我猜这是因为e.currentTarget.parentElement?.parentElement?.querySelectorAll('input')
可能未定义。 undefined
不能用作Array.from
的参数,因为undefined is not iterable
的。 在将其提供给Array.from
之前,您需要确保它存在。 两种可能的解决方案:
if (document.querySelector('.maybe')?.parentElement?.querySelectorAll('.doesnt-exist?')) {
const fields = Array.from(document.querySelector('.maybe')!.parentElement!.querySelectorAll('.doesnt-exist?'))
}
[]
,以防万一。 将该变量自动转换为HTMLInputElement[]
,然后可以在Array.from
中安全地使用它:const checker =
document.querySelector('.maybe')?.parentElement?.querySelectorAll('.doesnt-exist?') ?? [] as HTMLInputElement[]
const fields: Element[] = Array.from(checker)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.