Best Method to Cast a Number to String in TypeScript

What is the best way to cast a number to a string in TypeScript? Given the following example: var page_number: number = 3; window.location.hash = page_number;

The TypeScript compiler throws an error because location.hash expects a string, but page_number is a number.

I have two options to resolve this issue: window.location.hash = “” + page_number; // casting using “” literal window.location.hash = String(page_number); // casting using the String() function

Which method is better for performing a typescript cast to string?

In TypeScript, casting and conversion are two different concepts. Here’s the situation you’re dealing with:

You have:


var page_number: number = 3;

window.location.hash = page_number;

And since window.location.hash expects a string, TypeScript complains when you pass a number. To handle this properly, we need to convert the number to a string.

You have two common ways to convert the number to a string:


window.location.hash = "" + page_number; // using an empty string

window.location.hash = String(page_number); // using the String() function

Both methods work and handle runtime conversions. If page_number were null or undefined, they’d safely convert it without throwing errors.

However, page_number.toString() or page_number.toLocaleString() would fail in the case of null or undefined, so it’s safer to use the two above methods in more dynamic cases.

If you’re interested in casting (though it’s generally less recommended), you could do:


window.location.hash = <string>page_number;

// or

window.location.hash = page_number as string;

But this doesn’t perform an actual conversion at runtime. TypeScript will still throw an error because you can’t assign a number to a string directly. To bypass this, you’d need a two-step cast, first to any and then to string:


window.location.hash = <string><any>page_number;

// or

window.location.hash = page_number as any as string;

In most cases, the best approach is String(page_number) because it safely converts both at runtime and satisfies TypeScript’s type-checking.

Another straightforward way to convert a number to a string is by using the .toString() method. This method explicitly converts the number to its string representation:


window.location.hash = page_number.toString();

The advantage of .toString() is that it’s more readable and commonly used. However, be cautious as .toString() will throw an error if page_number is null or undefined. So, it’s fine for situations where you’re sure your variable is a number.

While String(page_number) and "" + page_number are both safe in more dynamic cases where the value could be null or undefined, .toString() is still a clean, typescript cast to string approach when you’re certain about the variable’s state.

If you need locale-aware string conversion, consider using the .toLocaleString() method, which formats the number according to the user’s local settings:


window.location.hash = page_number.toLocaleString();

While this is more powerful than .toString(), especially if you need formatting for numbers (like adding commas or decimals based on locale), it’s typically overkill for simple conversions to a string.

Use this method when you want to represent the number with locale-specific formatting, but for simpler cases of typescript cast to string, using String(page_number) or .toString() is more appropriate.