简体   繁体   English

如何修复“预期的表达。 ts(1109)”在 VSCode 中?

[英]How do I fix “Expression expected. ts(1109)” in VSCode?

I encountered VSCode error "Expression expected. ts(1009)" while opening one of the reactjs file.我在打开 reactjs 文件之一时遇到了 VSCode 错误“Expression expected.ts(1009)”。 do and if were highlighted red. doif以红色突出显示。 But I can run the code in my local development environment and also no problem compiling.但是我可以在我的本地开发环境中运行代码并且编译也没有问题。 I guess this is due to VSCode Editor bug.我猜这是由于 VSCode 编辑器错误。

Other editor like Atom or Sublime Text are fine.其他编辑器如 Atom 或 Sublime Text 都很好。 I used ternary operation in React to solve it but things get really complicated if there are too many else if .我在 React 中使用了三元运算来解决它,但是如果有太多else if事情就会变得非常复杂。

<div className='form'>
    // Form stuffs
</div>
{
    do{
      if (hasBreakfast) {
        <span>Breakfast Included</span>
      } else if {
        <span>Breakfast Not Included</span>
        }
    }
}

Is there any methods to solve the warning in VSCode?有什么方法可以解决VSCode中的警告吗?

return (
  <Fragment>
  // JSX to render goes here...
  </Fragment>
);

I received the same error on the last line.我在最后一行收到了同样的错误。 I added the fragment element to force the return () to receive one parent element.我添加了片段元素以强制 return () 接收一个父元素。 You can also accomplish this by wrapping your return code is a 'div' element.您还可以通过将返回代码包装为“div”元素来实现此目的。 The issue there however, is you will have random empty 'div' s.然而,那里的问题是你将有随机的空 'div' s。

The docs explain it better than I can.文档比我能更好地解释它。 https://reactjs.org/docs/fragments.html https://reactjs.org/docs/fragments.html

do expressions, as used in the original question, are actually "valid" JSX/JS syntaxif you're using the babel syntax/plugin that supports the do expression proposal . 如果您使用支持do expression proposal 的 babel 语法/插件,那么原始问题中使用的do表达式实际上是“有效”的 JSX/JS 语法。 The reason VS Code is displeased is because TypeScript itself does not support the syntax . VS Code 不高兴的原因是因为TypeScript 本身不支持语法 If it's working in your development environment anyway,again that's probably Babel .如果它无论如何都可以在您的开发环境中工作,那可能也是 Babel

导入成本 VSCode 扩展对我来说遇到了这个错误,刚刚卸载修复了我的问题。

在我的情况下,我使用三元条件得到这个Expression expected.ts(1109)错误。

The if statement needs an expression to assert if that block of code should be used. if语句需要一个表达式来断言是否应该使用该代码块。

ex:前任:

if (a = true) {
  // do something
} else if (b = true) {
  // do something else
}

If you just want to render the Breakfast Not Included you can just use else without the if .如果您只想呈现Breakfast Not Included ,则可以使用else而不使用if Or do make it simpler you could make a ternary :或者让它更简单,你可以做一个三元

{
  hasBreakfast ? <span>Breakfast Included</span> : <span>Breakfast Not Included</span>;
}

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

相关问题 获取错误:表达式 expected.ts(1109) React js - Getting Error: Expression expected.ts(1109) React js 带有构造函数和 state 的 React 文件中的表达式 expected.ts(1109) 错误 - Expression expected.ts(1109) error in React file with constructor and state 在react typescript tsx文件中声明数组会给出“错误TS1109:预期表达式” - Declaring array in react typescript tsx file gives “error TS1109: Expression expected” 期望表达。 与埃斯林特和反应 - Expression expected. with eslint and react 预期声明或声明。 ts(1128) - Declaration or statement expected. ts(1128) 反应 typescript '=' 预期。 TS1005 - React typescript '=' expected. TS1005 聊天应用程序的此代码在下一行显示“预期表达”,我该如何解决? - This code for a chat app says "expression expected" for the following line, how do I fix this? &#39;=&gt;&#39; 预期。 用于 Redux 操作接口的 TS1005? - '=>' expected. TS1005 for Redux Action Interface? 预期的 ReactBoostrap 声明或声明。 TS1128 - ReactBoostrap Declaration or statement expected. TS1128 如何在 Next.js 应用程序的 Vercel 部署中修复此 ts-toolbelt TypeScript TypeError? - How do I fix this ts-toolbelt TypeScript TypeError on Vercel deployment for a Next.js app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM