[英]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. do
和if
以红色突出显示。 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.