{"id":2041,"date":"2024-05-23T22:57:54","date_gmt":"2024-05-23T14:57:54","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=2041"},"modified":"2024-05-23T22:59:14","modified_gmt":"2024-05-23T14:59:14","slug":"typescript-as-const","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=2041","title":{"rendered":"TypeScript &#8211; as const"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2024\/05\/1716476306.png\" alt=\"1716476306\" \/><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c<code>as const<\/code> \u662f\u4e00\u79cd\u7c7b\u578b\u65ad\u8a00\u7684\u7528\u6cd5\uff0c\u5b83\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u67d0\u4e2a\u4f4d\u7f6e\u7684\u7c7b\u578b\u89c6\u4e3a\u4e00\u4e2a\u5e38\u91cf\uff08<code>const<\/code>\uff09\u7c7b\u578b\u3002\u8fd9\u5728 TypeScript \u7684\u7c7b\u578b\u5b88\u536b\u548c\u7c7b\u578b\u7ec6\u5316\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5b57\u9762\u91cf\u7c7b\u578b\u6216\u786e\u4fdd\u67d0\u4e2a\u4f4d\u7f6e\u7684\u503c\u4e0d\u4f1a\u88ab\u4fee\u6539\u65f6\u3002<\/p>\n<h3><a id=\"%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u57fa\u672c\u7528\u6cd5<\/h3>\n<p>\u5f53\u4f60\u4f7f\u7528 <code>as const<\/code> \u65f6\uff0c\u4f60\u544a\u8bc9 TypeScript \u7f16\u8bd1\u5668\uff0c\u67d0\u4e2a\u4f4d\u7f6e\u7684\u503c\u662f\u5e38\u91cf\uff0c\u4e0d\u5e94\u8be5\u88ab\u91cd\u65b0\u8d4b\u503c\u3002<\/p>\n<pre><code class=\"language-typescript\">const config = {\n  apiEndpoint: &quot;https:\/\/api.example.com&quot;,\n  timeout: 5000,\n} as const;\n\n\/\/ \u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u5bfc\u81f4 TypeScript \u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a\u6839\u636e as const\uff0cconfig \u88ab\u89c6\u4e3a\u53ea\u8bfb\u7684\n\/\/ config.apiEndpoint = &quot;https:\/\/new-api.example.com&quot;;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>config<\/code> \u5bf9\u8c61\u88ab\u65ad\u8a00\u4e3a <code>as const<\/code>\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u7684\u5c5e\u6027 <code>apiEndpoint<\/code> \u548c <code>timeout<\/code> \u88ab\u63a8\u65ad\u4e3a\u53ea\u8bfb\u5c5e\u6027\u3002<\/p>\n<h3><a id=\"%E5%9C%A8%E7%B1%BB%E5%9E%8B%E5%AE%88%E5%8D%AB%E4%B8%AD%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5728\u7c7b\u578b\u5b88\u536b\u4e2d\u4f7f\u7528<\/h3>\n<p><code>as const<\/code> \u5728\u7c7b\u578b\u5b88\u536b\u4e2d\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd\u67d0\u4e2a\u4f4d\u7f6e\u7684\u503c\u662f\u5b57\u9762\u91cf\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"language-typescript\">function isStringLiteral(value: any): value is string {\n  return typeof value === &quot;string&quot; &amp;&amp; value !== __VUE_OPTIONS_API__;\n}\n\nfunction processValue(value: string | number | boolean) {\n  if (isStringLiteral(value)) {\n    \/\/ \u5728\u8fd9\u4e2a\u5757\u5185\uff0cTypeScript \u77e5\u9053 value \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\n    value.toLowerCase(); \/\/ \u6ca1\u6709\u9519\u8bef\n  }\n}\n<\/code><\/pre>\n<h3><a id=\"%E5%9C%A8%E6%B3%9B%E5%9E%8B%E4%B8%AD%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5728\u6cdb\u578b\u4e2d\u4f7f\u7528<\/h3>\n<p><code>as const<\/code> \u4e5f\u53ef\u4ee5\u4e0e\u6cdb\u578b\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u6cdb\u578b\u53c2\u6570\u662f\u5e38\u91cf\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"language-typescript\">type Constify&lt;T&gt; = T extends object ? { [K in keyof T]: T[K] as const } : T;\n\nconst config = {\n  apiEndpoint: &quot;https:\/\/api.example.com&quot;,\n  timeout: 5000,\n} as Constify&lt;typeof config&gt;;\n\n\/\/ \u540c\u4e0a\uff0c\u8fd9\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\n\/\/ config.apiEndpoint = &quot;https:\/\/new-api.example.com&quot;;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Constify<\/code> \u6cdb\u578b\u786e\u4fdd\u4e86 <code>config<\/code> \u5bf9\u8c61\u7684\u6bcf\u4e2a\u5c5e\u6027\u90fd\u88ab\u63a8\u65ad\u4e3a\u5e38\u91cf\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8as-const%E7%9A%84%E5%A5%BD%E5%A4%84\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 <code>as const<\/code> \u7684\u597d\u5904<\/h3>\n<ul>\n<li><strong>\u7c7b\u578b\u5b89\u5168\u6027<\/strong>\uff1a<code>as const<\/code> \u589e\u52a0\u4e86\u4ee3\u7801\u7684\u7c7b\u578b\u5b89\u5168\u6027\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u503c\u4e0d\u4f1a\u88ab\u610f\u5916\u4fee\u6539\u3002<\/li>\n<li><strong>\u7c7b\u578b\u63a8\u65ad<\/strong>\uff1a\u5b83\u5e2e\u52a9 TypeScript \u7f16\u8bd1\u5668\u66f4\u51c6\u786e\u5730\u63a8\u65ad\u7c7b\u578b\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5bf9\u8c61\u548c\u6570\u7ec4\u5b57\u9762\u91cf\u65f6\u3002<\/li>\n<li><strong>\u7c7b\u578b\u5b88\u536b<\/strong>\uff1a\u5728\u4f7f\u7528\u7c7b\u578b\u5b88\u536b\u65f6\uff0c<code>as const<\/code> \u53ef\u4ee5\u5e2e\u52a9\u7f16\u8bd1\u5668\u7406\u89e3\u67d0\u4e2a\u4f4d\u7f6e\u7684\u503c\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/li>\n<\/ul>\n<p><code>as const<\/code> \u662f TypeScript \u4e2d\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u529f\u80fd\uff0c\u5b83\u5728 TypeScript 3.4 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u53ef\u7528\u3002\u901a\u8fc7\u4f7f\u7528 <code>as const<\/code>\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u5b89\u5168\u3001\u66f4\u53ef\u9884\u6d4b\u7684\u7c7b\u578b\u4ee3\u7801\u3002<\/p>\n<p><code>as const<\/code> \u662f TypeScript \u4e2d\u7684\u4e00\u79cd\u7c7b\u578b\u65ad\u8a00\u8bed\u6cd5\uff0c\u7528\u4e8e\u5c06\u8868\u8fbe\u5f0f\u65ad\u8a00\u4e3a\u53ea\u8bfb\uff08readonly\uff09\u7684\u5b57\u9762\u91cf\u7c7b\u578b\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u544a\u8bc9 TypeScript \u7f16\u8bd1\u5668\uff0c\u67d0\u4e2a\u8868\u8fbe\u5f0f\u5e94\u8be5\u88ab\u89c6\u4e3a\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u5e38\u91cf\u503c\uff0c\u800c\u4e0d\u662f\u53ef\u53d8\u7684\u53d8\u91cf\u3002<\/p>\n<h3><a id=\"%E7%A4%BA%E4%BE%8B%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u793a\u4f8b\uff1a<\/h3>\n<pre><code class=\"language-typescript\">const person = {\n  name: 'Alice',\n  age: 30,\n} as const;\n\n\/\/ person \u53d8\u91cf\u88ab\u65ad\u8a00\u4e3a\u53ea\u8bfb\u7684\u5b57\u9762\u91cf\u7c7b\u578b\n\/\/ \u56e0\u6b64\uff0c\u5c1d\u8bd5\u4fee\u6539\u5176\u5c5e\u6027\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\n\/\/ person.name = 'Bob'; \/\/ Error: Cannot assign to 'name' because it is a read-only property\n\/\/ person.age = 31; \/\/ Error: Cannot assign to 'age' because it is a read-only property\n\nconsole.log(person); \/\/ Output: { name: 'Alice', age: 30 }\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>person<\/code> \u88ab\u65ad\u8a00\u4e3a\u53ea\u8bfb\u7684\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u4fee\u6539\u5b83\u7684\u5c5e\u6027\u3002\u8fd9\u4e2a\u7279\u6027\u5bf9\u4e8e\u786e\u4fdd\u4e00\u4e9b\u5e38\u91cf\u503c\u5728\u7a0b\u5e8f\u4e2d\u4e0d\u88ab\u610f\u5916\u4fee\u6539\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h3><a id=\"%E7%94%A8%E9%80%94%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7528\u9014\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u5e38\u91cf\u5bf9\u8c61\uff1a<\/strong> \u5c06\u5bf9\u8c61\u5b57\u9762\u91cf\u65ad\u8a00\u4e3a\u53ea\u8bfb\u7684\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u4e0d\u88ab\u4fee\u6539\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5e38\u91cf\u6570\u7ec4\uff1a<\/strong> \u5c06\u6570\u7ec4\u5b57\u9762\u91cf\u65ad\u8a00\u4e3a\u53ea\u8bfb\u7684\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u4ee5\u786e\u4fdd\u6570\u7ec4\u5143\u7d20\u4e0d\u88ab\u4fee\u6539\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\uff1a<\/strong> \u5c06\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u65ad\u8a00\u4e3a\u53ea\u8bfb\u7684\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u7684\u503c\u5728\u7a0b\u5e8f\u4e2d\u4e0d\u88ab\u4fee\u6539\u3002<\/p>\n<\/li>\n<\/ol>\n<h3><a id=\"%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n<ul>\n<li><code>as const<\/code> \u65ad\u8a00\u53ea\u9002\u7528\u4e8e\u5b57\u9762\u91cf\u7c7b\u578b\uff08\u4f8b\u5982\u5bf9\u8c61\u5b57\u9762\u91cf\u3001\u6570\u7ec4\u5b57\u9762\u91cf\u3001\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7b49\uff09\uff0c\u5bf9\u4e8e\u5176\u4ed6\u8868\u8fbe\u5f0f\u4e0d\u8d77\u4f5c\u7528\u3002<\/li>\n<li>\u4f7f\u7528 <code>as const<\/code> \u65ad\u8a00\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u66f4\u52a0\u4e25\u683c\uff0c\u4f46\u4e5f\u4f1a\u964d\u4f4e\u7075\u6d3b\u6027\u3002\u786e\u4fdd\u5728\u5fc5\u8981\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u5b83\u3002<\/li>\n<\/ul>\n<p>\u603b\u7684\u6765\u8bf4\uff0c<code>as const<\/code> \u662f\u4e00\u79cd\u5728 TypeScript \u4e2d\u7528\u4e8e\u786e\u4fdd\u5e38\u91cf\u503c\u4e0d\u88ab\u4fee\u6539\u7684\u673a\u5236\uff0c\u7279\u522b\u9002\u7528\u4e8e\u786e\u4fdd\u5bf9\u8c61\u3001\u6570\u7ec4\u548c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u4e0d\u53ef\u53d8\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 TypeScript \u4e2d\uff0cas const \u662f\u4e00\u79cd\u7c7b\u578b\u65ad\u8a00\u7684\u7528\u6cd5\uff0c\u5b83\u7528\u4e8e\u544a\u8bc9\u7f16\u8bd1\u5668\u5c06\u67d0\u4e2a\u4f4d\u7f6e\u7684\u7c7b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,3],"tags":[33],"class_list":["post-2041","post","type-post","status-publish","format-standard","hentry","category-frontend","category-tech","tag-typescript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2041"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2042,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2041\/revisions\/2042"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}