Casting a number to a string in TypeScript

JavascriptCastingTypescript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

"Casting" is different than conversion. In this case, window.location.hash will auto-convert a number to a string. But to avoid a TypeScript compile error, you can do the string conversion yourself:

window.location.hash = ""+page_number; 
window.location.hash = String(page_number); 

These conversions are ideal if you don't want an error to be thrown when page_number is null or undefined. Whereas page_number.toString() and page_number.toLocaleString() will throw when page_number is null or undefined.

When you only need to cast, not convert, this is how to cast to a string in TypeScript:

window.location.hash = <string>page_number; 
// or 
window.location.hash = page_number as string;

The <string> or as string cast annotations tell the TypeScript compiler to treat page_number as a string at compile time; it doesn't convert at run time.

However, the compiler will complain that you can't assign a number to a string. You would have to first cast to <any>, then to <string>:

window.location.hash = <string><any>page_number;
// or
window.location.hash = page_number as any as string;

So it's easier to just convert, which handles the type at run time and compile time:

window.location.hash = String(page_number); 

(Thanks to @RuslanPolutsygan for catching the string-number casting issue.)

Solution 2 - Javascript

Utilize toString() or toLocaleString(), for example:

var page_number:number = 3;
window.location.hash = page_number.toLocaleString();

These throw an error if page_number is null or undefined. If you don't want that you can choose the fix appropriate for your situation:

// Fix 1:
window.location.hash = (page_number || 1).toLocaleString();

// Fix 2a:
window.location.hash = !page_number ? "1" page_number.toLocaleString();

// Fix 2b (allows page_number to be zero):
window.location.hash = (page_number !== 0 && !page_number) ? "1" page_number.toLocaleString();

Solution 3 - Javascript

One can also use the following syntax in typescript. Note the backtick " ` "

window.location.hash = `${page_number}`

Solution 4 - Javascript

This is some short ways

any_type = "" + any_type; 
any_type = String(any_type); 
any_type = `${any_type}`;

Solution 5 - Javascript

window.location.hash is a string, so do this:

var page_number: number = 3;
window.location.hash = String(page_number); 

Solution 6 - Javascript

> const page_number = 3; > > window.location.hash = page_number as string; // Error

"Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first." -> You will get this error if you try to typecast number to string. So, first convert it to unknown and then to string.

> window.location.hash = (page_number as unknown) as string; // Correct way

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMa JerezView Question on Stackoverflow
Solution 1 - JavascriptRobert PennerView Answer on Stackoverflow
Solution 2 - JavascriptJeroenView Answer on Stackoverflow
Solution 3 - JavascriptNehal DamaniaView Answer on Stackoverflow
Solution 4 - JavascriptBinh HoView Answer on Stackoverflow
Solution 5 - JavascriptraneshuView Answer on Stackoverflow
Solution 6 - JavascriptAnant RajView Answer on Stackoverflow