首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在TypeScript中将数字转换为字符串

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

Stack Overflow用户
提问于 2015-09-14 05:15:49
回答 6查看 397.5K关注 0票数 233

在Typescript中从数字转换为字符串的最佳方法(如果有)是什么?

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

在这种情况下,编译器抛出错误:

类型“number”不能赋值给类型“string”

因为location.hash是一个字符串。

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

那么哪种方法更好呢?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32554624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档