Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In TypeScript, How to cast boolean to number, like 0 or 1

Tags:

As we know, the type cast is called assertion type in TypeScript. And the following code section:

// the variable will change to true at onetime
let isPlay: boolean = false;
let actions: string[] = ['stop', 'play'];
let action: string = actions[<number> isPlay];

On compiling, it go wrong

Error:(56, 35) TS2352: Neither type 'boolean' nor type 'number' is assignable to the other.

Then I try to use the any type:

let action: string = actions[<number> <any> isPlay];

Also go wrong. How can I rewrite those code.

like image 482
a2htray yuen Avatar asked Apr 28 '17 19:04

a2htray yuen


People also ask

Can you cast a boolean to an int?

Using the method booleanObjectMethodToInt, we can convert a boolean value to an integer the same way we did with the static method. Similarly, you can apply the reversed version by changing the argument to true and adding 1 to the result.

How do you cast a boolean?

You can use CAST() to convert any integer or floating-point type to BOOLEAN : a value of 0 represents false , and any non-zero value is converted to true . You can cast DECIMAL values to BOOLEAN , with the same treatment of zero and non-zero values as the other numeric types. You cannot cast a BOOLEAN to a DECIMAL .

Can a boolean be a Number?

A Boolean number has value True/ON and False/OFF; therefore, when a Boolean is converted into a number it would return 1 or 0 for True/ON or False/OFF respectively.

How to convert Boolean to number in typescript?

Today, we will know the easiest way to convert boolean to number in typescript. TypeScript has + unary operator which helps you to convert boolean to the number when we add a unary operator with true it will return 1, and with false it will return 0.

What is the difference between Boolean and primitive in typescript?

The Boolean is an object wrapper for the primitive boolean value. Also, let us find the difference between Boolean object vs boolean primitive. The Typescript also has a Boolean global function, which we use to convert any value to boolean primitive type.

What are the data types supported by typescript?

TypeScript Data Type - Boolean. Boolean values are supported by both JavaScript and TypeScript and stored as true/false values. TypeScript Boolean: let isPresent:boolean = true; Note that, Boolean with an upper case B is different from boolean with a lower case b. Upper case Boolean is an object type whereas lower case boolean is a primitive type.

How to cast a number to a Boolean in JavaScript?

While you can't cast a number directly to a boolean, you can cast it to the wrapper Boolean class and immediately unwrap it. For example: foo(<boolean><Boolean>xxx); While clumsy, it avoids the type erasure of casting to <any>. It's also arguably less obscure & more readable than the !!approach (certainly so in the transpiled js code). Share


1 Answers

You can't just cast it, the problem is at runtime not only at compile time.

You have a few ways of doing that:

let action: string = actions[isPlay ? 1 : 0];
let action: string = actions[+isPlay];
let action: string = actions[Number(isPlay)];

Those should be fine with both the compiler and in runtime.

like image 187
Nitzan Tomer Avatar answered Sep 21 '22 17:09

Nitzan Tomer