简体   繁体   English

“解析错误:预期的参数表达式。eslint”与 React 中的 Typescript 映射函数

[英]"Parsing error: Argument expression expected. eslint" with Typescript map function in React

From this line of code从这行代码

{inputFields.map(if => <Input inputField={if} /> )}

I am getting this error on the word map我在单词地图上收到此错误

Parsing error: Argument expression expected.eslint

inputFields is inside a form, which is inside a div inputFields 位于表单内,该表单位于 div 内

<div>
    <form>
        {inputFields.map...}
    </form>
</div

And input fields is an array输入字段是一个数组

inputFields: InputField[]

This answer suggests that this could have something to do with the typescript version.这个答案表明这可能与打字稿版本有关。

In my package.json , I have that I'm using "typescript": "^3.8.3" , but in the bottom right, I think it's saying that I'm using version 4.0.2?在我的package.json ,我使用的是"typescript": "^3.8.3" ,但在右下角,我认为它是在说我使用的是 4.0.2 版?

在此处输入图片说明

When I click on that number, I can change the number to 3.9.7 but that doesn't change the error当我点击那个数字时,我可以将数字更改为3.9.7但这不会改变错误


Other answers that I've seen are not relevant to my use case with the map function我看到的其他答案与我使用 map 函数的用例无关

if is a keyword in Javascript. if是 Javascript 中的关键字。 Try renaming your variable:尝试重命名您的变量:

{inputFields.map(field => <Input inputField={field} /> )}

Statements vs Expressions语句与表达式

An expression is code that resolves to a value表达式是解析为值的代码

const add = (a, b) => a + b
add(10, 20) // => 30 
'Hello' // => 'Hello'

A statement is code that makes something happen语句是使某事发生的代码

if (x < 10) // doesn't resolve
break // doesn't resolve
switch // doesn't resolve

Therefore the warning: "“Parsing error: Argument expression expected. eslint” is telling you that an expression (something that resolves to a value) is expected but instead you have passed a statement (which cannot be resolved to a value)因此,警告:““解析错误:期望的参数表达式。eslint”告诉您需要一个表达式(解析为值的东西),但您已经传递了一个语句(无法解析为值)

You cannot assign a statement to a variable, only expressions.您不能将语句分配给变量,只能分配表达式。

您不能使用带有关键字的变量,例如 if 或 var 作为 js 中的变量,因此您需要重命名变量的名称,因为它应该可以工作

{inputFields.map((data)=> <Input inputField={data} /> )}

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

相关问题 期望表达。 与埃斯林特和反应 - Expression expected. with eslint and react 解析错误:“,”预期。 具有数组映射功能 - Parsing error: ',' expected. with array map function IONIC / REACT 第 9:5 行:期望一个赋值或函数调用,而是看到一个表达式 @typescript-eslint/no-unused-expressions - IONIC / REACT Line 9:5: Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions 期望分配或 function 调用,而是看到一个表达式 @typescript-eslint/no-unused-expressions (React) - Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions (React) 反应打字稿解析错误:“{”预期 - React typescript Parsing error: '{' expected 反应 typescript '=' 预期。 TS1005 - React typescript '=' expected. TS1005 ESLint React解析错误 - ESLint React Parsing Error React typescript eslint 错误解析错误:意外的令牌? 如何解决这个问题? - React typescript eslint error Parsing error: Unexpected token ! How to fix this? 反应打字稿错误解析错误:&#39;&gt;&#39;预期 - React typescript error Parsing error: '>' expected 解析错误:意外的令牌,预期的“,” - 与 Typescript 反应 - Parsing error: Unexpected token, expected “,” - React with Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM