{"id":52356,"date":"2024-12-03T13:56:42","date_gmt":"2024-12-03T05:56:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52356\/"},"modified":"2024-12-03T13:56:42","modified_gmt":"2024-12-03T05:56:42","slug":"typescript%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b%e7%ba%a6%e6%9d%9f%ef%bc%9a%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e8%b7%af%e5%be%84%e6%8e%a8%e6%96%ad%e5%8f%82%e6%95%b0%e5%b9%b6%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52356\/","title":{"rendered":"TypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>TypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aTypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732070763673d4d6b8da6e.jpg\" class=\"aligncenter\" title=\"TypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f\u63d2\u56fe\" alt=\"TypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u51fd\u6570\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7c7b\u578b\uff0c\u63a8\u65ad\u6700\u7ec8\u7ed3\u679c<\/strong><\/p>\n<p>\u5728 typescript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u51fd\u6570\uff0c\u5176\u884c\u4e3a\u6839\u636e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u53d8\u5316\u800c\u6539\u53d8\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6cdb\u578b\u548c\u7c7b\u578b\u7ea6\u675f\u6765\u5b9e\u73b0\uff0c\u4ee5\u4fbf\u6839\u636e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u5408\u5e76\u8def\u5f84\u548c\u53c2\u6570\uff0c\u5e76\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\uff0c\u6700\u7ec8\u5f97\u51fa\u5408\u5e76\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u539f\u59cb\u5b9e\u73b0<\/strong><\/p>\n<pre>type fullurl&lt;\n  tpath extends string,\n  tparams extends record&lt;string, string&gt;,\n&gt; = `${tpath}${tparams extends record&lt;string, never&gt; ? '' : '?'}${buildquerystring&lt;tparams&gt;}`;<\/pre>\n<p><strong>\u4f18\u5316\u5b9e\u73b0<\/strong><\/p>\n<p>\u7136\u800c\uff0c\u539f\u59cb\u5b9e\u73b0\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u5bf9 union \u7c7b\u578b\u53c2\u6570\u7684\u63a8\u65ad\u4e0d\u51c6\u786e\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\uff1a<\/p>\n<pre>type finalbuildquerystring&lt;t extends record&lt;string, string&gt;&gt; = joinwithampersand&lt;\n  uniontotuple&lt;buildquerystring&lt;t&gt;&gt;\n&gt;;<\/pre>\n<p>\u8fd9\u79cd\u4fee\u6539\u786e\u4fdd\u4e86\u5bf9 union \u7c7b\u578b\u53c2\u6570\u7684\u6b63\u786e\u63a8\u65ad\u3002<\/p>\n<p>\u6700\u7ec8\u7684\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre>type fullurl&lt;\n  tpath extends string,\n  tparams extends record&lt;string, string&gt;,\n&gt; = `${tpath}${tparams extends record&lt;string, string&gt; ? `?${finalbuildquerystring&lt;tparams&gt;}` : ''}`;<\/pre>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u6539\u8fdb\u540e\u7684\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>const orderUrl = buildStringWithParams('\/order\/detail', orderParams);\nconst productListUrl = buildStringWithParams('\/product\/list', productListParams);<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5229\u7528\u6cdb\u578b\u548c\u7c7b\u578b\u7ea6\u675f\uff0c\u6211\u4eec\u80fd\u591f\u521b\u5efa\u51fd\u6570\uff0c\u6839\u636e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u63a8\u65ad\u7c7b\u578b\u548c\u751f\u6210\u52a8\u6001\u7684\u6700\u7ec8\u7ed3\u679c\u3002\u6211\u4eec\u4fee\u6539\u540e\u7684\u5b9e\u73b0\u89e3\u51b3\u4e86\u539f\u59cb\u5b9e\u73b0\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u5e76\u786e\u4fdd\u4e86\u7c7b\u578b\u5b89\u5168\u548c\u51c6\u786e\u7684\u63a8\u65ad\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aTypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u4eac\u4e1c\u65b9 MLED \u751f\u4ea7\u57fa\u5730\u843d\u6237\u73e0\u6d77\uff0c\u9884\u8ba1\u660e\u5e74\u4e00\u5b63\u5ea6\u5168\u9762\u91cf\u4ea7\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aTypeScript\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u7ea6\u675f\uff1a\u5982\u4f55\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\u5e76\u6784\u5efa\u5b8c\u6574URL\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u51fd\u6570\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7c7b\u578b\uff0c\u63a8\u65ad\u6700\u7ec8\u7ed3\u679c \u5728 typescript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u51fd\u6570\uff0c\u5176\u884c\u4e3a\u6839\u636e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u53d8\u5316\u800c\u6539\u53d8\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6cdb\u578b\u548c\u7c7b\u578b\u7ea6\u675f\u6765\u5b9e\u73b0\uff0c\u4ee5\u4fbf\u6839\u636e\u7b2c\u4e00\u4e2a\u53c2\u6570\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u5408\u5e76\u8def\u5f84\u548c\u53c2\u6570\uff0c\u5e76\u6839\u636e\u8def\u5f84\u63a8\u65ad\u53c2\u6570\uff0c\u6700\u7ec8\u5f97\u51fa\u5408\u5e76\u540e\u7684\u5b57\u7b26\u4e32\u3002 \u539f\u59cb\u5b9e\u73b0 type fullurl&lt; tpath extends string, tparams extends record&lt;string, string&gt;, &gt; = `${tpath}${tparams extends record&lt;string, never&gt; ? &#8221; : &#8216;?&#8217;}${buildquerystring&lt;tparams&gt;}`; \u4f18\u5316\u5b9e\u73b0 \u7136\u800c\uff0c\u539f\u59cb\u5b9e\u73b0\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u4f8b\u5982\u5bf9 union \u7c7b\u578b\u53c2\u6570\u7684\u63a8\u65ad\u4e0d\u51c6\u786e\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\uff1a type finalbuildquerystring&lt;t extends record&lt;string, string&gt;&gt; = joinwithampersand&lt; uniontotuple&lt;buildquerystring&lt;t&gt;&gt; &gt;; \u8fd9\u79cd\u4fee\u6539\u786e\u4fdd\u4e86\u5bf9 union \u7c7b\u578b\u53c2\u6570\u7684\u6b63\u786e\u63a8\u65ad\u3002 \u6700\u7ec8\u7684\u51fd\u6570\u5982\u4e0b\uff1a type fullurl&lt; tpath extends string, tparams extends record&lt;string, string&gt;, &gt; = [&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-52356","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52356","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=52356"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52356\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}