Casting a number to a string in TypeScript

Which is the the best way (if there is one) to cast from number to string in Typescript?

var page_number:number = 3;
window.location.hash = page_number; 

In this case the compiler throws the error:

Type ‘number’ is not assignable to type ‘string’

Because location.hash is a string.

window.location.hash = ""+page_number; //casting using "" literal
window.location.hash = String(number); //casting creating using the String() function

So which method is better?

7 Answers
7

Leave a Comment