一个在TypeScript中使用对象层次结构的if语句

huangapple go评论58阅读模式
英文:

An if statement using an object hierachy in Typescript

问题

我尝试使用以下层次结构构建逻辑语句:

if ((config.elementConfig.curve[0].dataset[0].splitBy = '我的离散变量')) {.....

我收到了来自TypeScript的错误消息:"错误 未预期的常量条件 no-constant-condition"。

我应该如何更改它,以使TypeScript接受它?

英文:

I am trying to base a logical statement using a hierachy like below

if ((config.elementConfig.curve[0].dataset[0].splitBy = 'my descrete var')) {.....

I get the error "error Unexpected constant condition no-constant-condition"
from typescript

how do I change it so typescript will like it

答案1

得分: 0

I suspect your issue is that you are trying to do a variable assignment, not an evaluation. You want to use ==, not =.

= is used to assign:

const myValue = "thing";

== is used to evaluate.

It's complaining that the expression you're giving it doesn't evaluate to something the if condition can check!
if (myValue === "thing") {

So you probably intend to have:

if ((config.elementConfig.curve[0].dataset[0].splitBy == 'my descrete var')
英文:

I suspect your issue is that you are trying to do a variable asignment, not an evaluation. You want to use ==, not =.

= is used to assign:

const myValue = "thing";

== is used to evaluate.


It's complaining that the expression you're giving it doesn't evaluate to something the if condition can check!
if (myValue === "thing") {

So you probably intend to have:

if ((config.elementConfig.curve[0].dataset[0].splitBy == 'my descrete var')

huangapple
  • 本文由 发表于 2023年2月9日 00:54:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定