{"id":28349,"date":"2024-11-24T16:47:19","date_gmt":"2024-11-24T08:47:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28349\/"},"modified":"2024-11-24T16:47:19","modified_gmt":"2024-11-24T08:47:19","slug":"typescript-%e4%b8%ad%e7%9a%84-as-number-%e4%b8%ba%e4%bb%80%e4%b9%88%e6%b2%a1%e6%9c%89%e6%94%b9%e5%8f%98%e5%8f%98%e9%87%8f%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28349\/","title":{"rendered":"TypeScript \u4e2d\u7684 as number \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6539\u53d8\u53d8\u91cf\u7c7b\u578b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173156247588440.jpg\" class=\"aligncenter\" title=\"TypeScript \u4e2d\u7684 as number \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6539\u53d8\u53d8\u91cf\u7c7b\u578b\uff1f\u63d2\u56fe\" alt=\"TypeScript \u4e2d\u7684 as number \u4e3a\u4ec0\u4e48\u6ca1\u6709\u6539\u53d8\u53d8\u91cf\u7c7b\u578b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u4e2d as number \u4e3a\u4f55\u4ecd\u7136\u662f\u5b57\u7b26\u4e32\uff1f<\/strong><\/p>\n<p>\u5728 TypeScript \u4e2d\u4f7f\u7528 as number \u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u540e\uff0c\u53d8\u91cf\u7c7b\u578b\u4e3a\u4f55\u4ecd\u663e\u793a\u4e3a\u5b57\u7b26\u4e32\uff1f<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>const props = defineProps&lt;{ group: number }&gt;()\n\ngetDictGroup(props.group)\n\nexport const getDictGroup = async (sid: number) =&gt; {\n  const dict = await getDict()\n  console.info(typeof sid)  \/\/ number\n  sid = sid as number        \/\/ \u671f\u5f85\u4e3a number\n  console.info(typeof (sid)) \/\/ string\n  console.info(typeof (sid as number)) \/\/ number\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u867d\u7136\u5728\u58f0\u660e\u548c\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u90fd\u6307\u5b9a\u4e86 sid \u4e3a number \u7c7b\u578b\uff0c\u4f46\u5b9e\u9645\u6253\u5370\u8f93\u51fa typeof sid \u5374\u663e\u793a\u4e3a string\u3002\u8fd9\u662f\u5462\uff1f<\/p>\n<p><strong>TypeScript \u7c7b\u578b\u8f6c\u6362\u7684\u672c\u8d28<\/strong><\/p>\n<p>TypeScript \u4e2d\u7684 as \u7c7b\u578b\u8f6c\u6362\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u7f16\u8bd1\u65f6\u68c0\u67e5\uff0c\u5b83\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u6267\u884c\u5b9e\u9645\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3a TypeScript \u662f JavaScript \u7684\u8d85\u96c6\uff0c\u800c JavaScript \u672c\u8eab\u4e0d\u5177\u5907\u771f\u6b63\u7684\u7c7b\u578b\u7cfb\u7edf\u3002<\/p>\n<p><strong>\u6b63\u786e\u7684\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u5728 JavaScript \u4e2d\u6267\u884c\u771f\u6b63\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre>let n = 12345\nn = String(n)\nconsole.log(n) \/\/ \"12345\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06\u539f\u59cb\u6570\u5b57\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\uff0c\u8fd8\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>Number(value)<\/strong>\uff1a\u5c06\u503c\u8f6c\u6362\u4e3a\u6570\u5b57<\/li>\n<li> <strong>String(value)<\/strong>\uff1a\u5c06\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/li>\n<li> <strong>Boolean(value)<\/strong>\uff1a\u5c06\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c<\/li>\n<\/ul>\n<p>\u603b\u4e4b\uff0c\u5728 TypeScript \u4e2d\u4f7f\u7528 as \u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u6539\u53d8\u53d8\u91cf\u7684\u5b9e\u9645\u7c7b\u578b\u3002\u8981\u8fdb\u884c\u771f\u6b63\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u5fc5\u987b\u4f7f\u7528\u6b63\u786e\u7684\u8f6c\u6362\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fTypeScript \u4e2d\u7684 as number \u6ca1\u6709\u6539\u53d8\u53d8\u91cf\u7c7b\u578b\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e2d as number \u4e3a\u4f55\u4ecd\u7136\u662f\u5b57\u7b26\u4e32\uff1f \u5728 TypeScript \u4e2d\u4f7f\u7528 as number \u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u540e\uff0c\u53d8\u91cf\u7c7b\u578b\u4e3a\u4f55\u4ecd\u663e\u793a\u4e3a\u5b57\u7b26\u4e32\uff1f \u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a const props = defineProps&lt;{ group: number }&gt;() getDictGroup(props.group) export const getDictGroup = async (sid: number) =&gt; { const dict = await getDict() console.info(typeof sid) \/\/ number sid = sid as number \/\/ \u671f\u5f85\u4e3a number console.info(typeof (sid)) \/\/ string console.info(typeof (sid as number)) \/\/ number [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-28349","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28349"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28349\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}