How to convert string type to number type in TypeScript

How to convert string type to number type in TypeScript

This is an easy way to convert string → number in TypeScript.

let a:string = '100';
let b:any = +a;// add + to it
console.log(typeof b);// displayed as number

|0 is not available.

With JS, I could convert it with |0 as follows, but it seems impossible with TS.

let abd:number = '100'|0;
console.log(typeof abd);

It will be “TS2362: The left-hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type.

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL