Javascript 在 TypeScript 中将数字转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32554624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:36:22  来源:igfitidea点击:

Casting a number to a string in TypeScript

javascriptcastingtypescript

提问by Ma Jerez

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

在 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.hashis a string.

因为location.hash是字符串。

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

So which method is better?

那么哪种方法更好呢?

回答by Robert Penner

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

“铸造”不同于转换。在这种情况下,window.location.hash将自动将数字转换为字符串。但是为了避免 TypeScript 编译错误,您可以自己进行字符串转换:

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_numberis nullor undefined. Whereas page_number.toString()and page_number.toLocaleString()will throw when page_numberis nullor undefined.

如果您不希望在page_numberisnull或时抛出错误,这些转换是理想的undefined。而page_number.toString()andpage_number.toLocaleString()会在page_numberis nullor时抛出undefined

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

当您只需要转换而不是转换时,这是在 TypeScript 中转换为字符串的方法:

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

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

<string>as string投注解告诉打字稿编译器把page_number在编译时间的字符串; 它不会在运行时转换。

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>:

但是,编译器会抱怨您无法为字符串分配数字。您必须先转换为<any>,然后转换为<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.)

(感谢@RuslanPolutsygan 捕获字符串数字转换问题。)

回答by Jeroen

Just utilize toStringor toLocaleStringI'd say. So:

只是利用toString或者toLocaleString我会说。所以:

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


These throw an error if page_numberis nullor undefined. If you don't want that you can choose the fix appropriate for your situation:

如果page_numbernull或 ,则抛出错误undefined。如果您不想这样做,可以选择适合您情况的修复程序:

// 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();

回答by Nehal Damania

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

还可以在打字稿中使用以下语法。注意反引号“`”

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

回答by raneshu

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

window.location.hash 是一个string,所以这样做:

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

回答by Anant Raj

const page_number = 3;

window.location.hash = page_number as string; // Error

const page_number = 3;

window.location.hash = page_number 作为字符串;// 错误

"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.

“将 'number' 类型转换为 'string' 类型可能是一个错误,因为这两种类型都没有与另一个类型充分重叠。如果这是故意的,请先将表达式转换为 'unknown'。” -> 如果您尝试将数字类型转换为字符串,则会出现此错误。因此,首先将其转换为未知,然后再转换为字符串。

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

window.location.hash = (page_number as unknown) as string; //正确的方法

回答by Bettaibi Nidhal

Use the "+" symbol to cast a string to a number.

使用“+”符号将字符串转换为数字。

window.location.hash = +page_number;