{"id":28380,"date":"2024-11-24T14:53:27","date_gmt":"2024-11-24T06:53:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28380\/"},"modified":"2024-11-24T14:53:27","modified_gmt":"2024-11-24T06:53:27","slug":"typescript-%e4%b8%ad-as-number-%e7%9c%9f%e7%9a%84%e4%bc%9a%e8%bd%ac%e6%8d%a2%e7%b1%bb%e5%9e%8b%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28380\/","title":{"rendered":"TypeScript \u4e2d as number \u771f\u7684\u4f1a\u8f6c\u6362\u7c7b\u578b\u5417\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173158795622151.jpg\" class=\"aligncenter\" title=\"TypeScript \u4e2d as number \u771f\u7684\u4f1a\u8f6c\u6362\u7c7b\u578b\u5417\uff1f\u63d2\u56fe\" alt=\"TypeScript \u4e2d as number \u771f\u7684\u4f1a\u8f6c\u6362\u7c7b\u578b\u5417\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u4e2d as number \u7c7b\u578b\u8f6c\u6362\u7684\u672c\u8d28<\/strong><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0cas number \u7c7b\u578b\u8f6c\u6362\u5e38\u7528\u4e8e\u5f3a\u8f6c\u4e00\u4e2a\u503c\u7c7b\u578b\u4e3a\u6570\u5b57\u3002\u4e0d\u8fc7\uff0c\u4e0e\u9884\u671f\u4e0d\u540c\u7684\u662f\uff0c\u8be5\u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u5b9e\u9645\u8f6c\u6362\u503c\u3002<\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\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); \/\/ \"string\"\n  sid = sid as number;\n  console.info(typeof sid); \/\/ \"number\"\n  console.info(typeof (sid as number)); \/\/ \"number\"\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u4f60\u6240\u89c1\uff0c\u5373\u4f7f\u5728\u591a\u6b21\u58f0\u660e sid \u4e3a\u6570\u5b57\u7c7b\u578b\uff0c\u6253\u5370\u51fa\u6765\u7684\u7c7b\u578b\u4ecd\u7136\u662f\u5b57\u7b26\u4e32\u3002\u8fd9\u662f\u56e0\u4e3a as number \u6b3a\u9a97\u4e86\u7f16\u8bd1\u5668\uff0c\u800c\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u6267\u884c\u771f\u6b63\u7684\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>\u6b63\u786e\u7684 TypeScript \u7c7b\u578b\u8f6c\u6362\u5e94\u4f7f\u7528 Number() \u6216 parseInt() \u51fd\u6570\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); \/\/ \"string\"\n  \/\/ \u4f7f\u7528 Number \u8f6c\u6362\n  sid = Number(sid);\n  \/\/ \u4f7f\u7528 parseInt \u8f6c\u6362\n  sid = parseInt(sid);\n  console.info(typeof sid); \/\/ \"number\"\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fTypeScript \u4e2d as number \u771f\u7684\u4f1a\u8f6c\u6362\u7c7b\u578b\u5417\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 \u7c7b\u578b\u8f6c\u6362\u7684\u672c\u8d28 \u5728 TypeScript \u4e2d\uff0cas number \u7c7b\u578b\u8f6c\u6362\u5e38\u7528\u4e8e\u5f3a\u8f6c\u4e00\u4e2a\u503c\u7c7b\u578b\u4e3a\u6570\u5b57\u3002\u4e0d\u8fc7\uff0c\u4e0e\u9884\u671f\u4e0d\u540c\u7684\u662f\uff0c\u8be5\u7c7b\u578b\u8f6c\u6362\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u5b9e\u9645\u8f6c\u6362\u503c\u3002 \u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\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); \/\/ &#8220;string&#8221; sid = sid as number; console.info(typeof sid); \/\/ &#8220;number&#8221; console.info(typeof (sid as number)); \/\/ &#8220;number&#8221; }; \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f60\u6240\u89c1\uff0c\u5373\u4f7f\u5728\u591a\u6b21\u58f0\u660e sid [&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-28380","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28380","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=28380"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28380\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}