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.

コメント

タイトルとURLをコピーしました