Why I get error when I try to declare local variable?

Michael

I bew to the typescript. In my function in the component I try to declare a local variable "contains":

  setUpgrade($event) {
    
     contains : Boolean = this.selectedUpgrades.includes($event);

    //some logic
    
    }

but on this row:

 contains : Boolean = this.selectedUpgrades.includes($event);   

on the Boolean I get this error:

  Type 'boolean' is not assignable to type 'BooleanConstructor'.
  

Why I get an error on the variable declaration?

CertainPerformance

The Boolean type is something that you'd get if you invoked the Boolean constructor:

const somethingWeird = new Boolean(Math.random() < 0.5);

This is almost never what you want - it can behave quite oddly. For example, a Boolean object containing false is truthy. Its typeof is also object, not boolean. Use the boolean type instead.

const contains : boolean = this.selectedUpgrades.includes($event);

Or, even better, leave out the type annotation entirely, and let TS infer it automatically:

const contains = this.selectedUpgrades.includes($event);

That's usually preferable, since it leads to less boilerplate and less code that needs to be read.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

Why I get error when I try to create stored procedure?

Why do I get an MKErrorDomain error when doing a local search?

Why is there still a duplicate error even when I declare the variable in 2 different scopes?

Why do i always get NULL in php response when i try to send some variable using ajax

Why I get error when try append element to document?

Why Do I get an error when I try to get tkinter slider value?

React App: Why I get an error when I try to run npm start script?

Why did i get every time an Syntax error when i try to put an Variable in an Xpath

Why do I get an error when create a function but not when a variable?

Why when I attempt to reassign a variable I receive the error, "remove this useless assignment to local variable"

Why am I getting the error "using uninitialized memory 'i'" and "uninitialized local variable 'i' used" when trying to make i = i*i

I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why?

When I try to redefine a variable, I get an index out of bounds error

Why do I get error when I try to alert the length of the passed array to the function?

Why don't I get a warning when I declare a variable with same name in a macro that already exists in a function?

I get this error when I try to install

Why do I get an undefined local variable or method error when using a constant, but not when using a method?

Why do I get 'undefined' error when I try to read session atrribute from Controller

Error when I try to enter on local adress

Why do I get a NoClassDefFound error when I try to save my test plan?

Why I get error when I try to install pyaudio?

Why do I get a "must declare scalar variable" error in dynamic SQL?

Why do I get an error when I try to see my Text using useState?

Why I get an error when I try to upload a picture

Why do I get error when try to convert Carbon to DateTime?

Why I get error when try build image in docker?

When I try to declare a global variable it throws the error Statement expected

Why do I get a lifetime error when I store closure as a variable and no error when I declare it as an expression?

TOP Ranking

HotTag

Archive