{"id":2017,"date":"2024-05-13T21:03:23","date_gmt":"2024-05-13T13:03:23","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=2017"},"modified":"2024-05-13T21:04:02","modified_gmt":"2024-05-13T13:04:02","slug":"ts-zi-mian-liang-lei-xing","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=2017","title":{"rendered":"TS &#8211; \u5b57\u9762\u91cf\u7c7b\u578b"},"content":{"rendered":"<p>\u5728 TypeScript \u4e2d\uff0c\u5b57\u9762\u91cf\u7c7b\u578b\uff08Literal Types\uff09\u662f\u6307\u90a3\u4e9b\u4e0e\u7279\u5b9a\u5b57\u9762\u91cf\u503c\u4e25\u683c\u5bf9\u5e94\u7684\u7c7b\u578b\u3002\u5b57\u9762\u91cf\u7c7b\u578b\u5305\u62ec\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\u3001\u6570\u5b57\u5b57\u9762\u91cf\u7c7b\u578b\u548c\u5e03\u5c14\u5b57\u9762\u91cf\u7c7b\u578b\u3002\u4f7f\u7528\u5b57\u9762\u91cf\u7c7b\u578b\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u51c6\u786e\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u5b83\u4eec\u9650\u5236\u53d8\u91cf\u53ea\u80fd\u8d4b\u503c\u4e3a\u7279\u5b9a\u7684\u5b57\u9762\u91cf\u3002<\/p>\n<h3><a id=\"%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AD%97%E9%9D%A2%E9%87%8F%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b<\/h3>\n<p>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u53ea\u80fd\u662f\u7279\u5b9a\u5b57\u7b26\u4e32\u503c\u7684\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"language-typescript\">type EventType = &quot;click&quot; | &quot;mouseover&quot; | &quot;keydown&quot;;\nconst eventType: EventType = &quot;click&quot;;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>EventType<\/code> \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u5b83\u53ea\u80fd\u662f <code>&quot;click&quot;<\/code>\u3001<code>&quot;mouseover&quot;<\/code> \u6216 <code>&quot;keydown&quot;<\/code> \u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n<h3><a id=\"%E6%95%B0%E5%AD%97%E6%95%B0%E9%9D%A2%E9%87%8F%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6570\u5b57\u6570\u9762\u91cf\u7c7b\u578b<\/h3>\n<p>\u6570\u5b57\u5b57\u9762\u91cf\u7c7b\u578b\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u53ea\u80fd\u662f\u7279\u5b9a\u6570\u5b57\u503c\u7684\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"language-typescript\">type StatusCode = 200 | 301 | 404 | 500;\nconst status: StatusCode = 200;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>StatusCode<\/code> \u662f\u4e00\u4e2a\u6570\u5b57\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u5b83\u53ea\u80fd\u662f <code>200<\/code>\u3001<code>301<\/code>\u3001<code>404<\/code> \u6216 <code>500<\/code> \u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n<h3><a id=\"%E5%B8%83%E5%B0%94%E5%AD%97%E9%9D%A2%E9%87%8F%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5e03\u5c14\u5b57\u9762\u91cf\u7c7b\u578b<\/h3>\n<p>\u5e03\u5c14\u5b57\u9762\u91cf\u7c7b\u578b\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u53ea\u80fd\u662f\u5e03\u5c14\u503c <code>true<\/code> \u6216 <code>false<\/code> \u7684\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"language-typescript\">type BooleanValue = true | false;\nconst isReady: BooleanValue = false;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>isReady<\/code> \u53d8\u91cf\u53ea\u80fd\u88ab\u8d4b\u503c\u4e3a <code>true<\/code> \u6216 <code>false<\/code>\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5b57\u9762\u91cf\u7c7b\u578b\u5728\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\u7684\u66ff\u4ee3\u54c1\u3001\u9650\u5236\u5b57\u7b26\u4e32\u6216\u6570\u5b57\u53ea\u80fd\u4e3a\u7279\u5b9a\u503c\u65f6\u975e\u5e38\u6709\u7528\u3002\u5b83\u4eec\u4e5f\u5e38\u7528\u4e8e\u7c7b\u578b\u5b88\u536b\u4e2d\uff0c\u4ee5\u786e\u4fdd\u53d8\u91cf\u7684\u503c\u7b26\u5408\u9884\u671f\u3002<\/p>\n<pre><code class=\"language-typescript\">interface User {\n  name: string;\n  onlineStatus: &quot;online&quot; | &quot;offline&quot;;\n}\n\nfunction printUserStatus(user: User) {\n  if (user.onlineStatus === &quot;online&quot;) {\n    console.log(`${user.name} is online.`);\n  } else {\n    console.log(`${user.name} is offline.`);\n  }\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>onlineStatus<\/code> \u5b57\u6bb5\u4f7f\u7528\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u5b83\u9650\u5236\u4e86 <code>onlineStatus<\/code> \u53ea\u80fd\u662f <code>&quot;online&quot;<\/code> \u6216 <code>&quot;offline&quot;<\/code>\u3002<\/p>\n<h3><a id=\"%E4%B8%8Eenum%E7%9A%84%E5%BC%82%E5%90%8C\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e0e enum \u7684\u5f02\u540c<\/h3>\n<p>\u5b57\u9762\u91cf\u7c7b\u578b\uff08Literal Types\uff09\u548c\u679a\u4e3e\uff08Enums\uff09\u5728 TypeScript \u4e2d\u90fd\u7528\u4e8e\u9650\u5236\u53d8\u91cf\u7684\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u533a\u522b\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5b83\u4eec\u7684\u5f02\u540c\uff0c\u5e76\u901a\u8fc7\u4f8b\u5b50\u52a0\u4ee5\u8bf4\u660e\u3002<\/p>\n<h3><a id=\"%E7%9B%B8%E5%90%8C%E4%B9%8B%E5%A4%84%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u76f8\u540c\u4e4b\u5904\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u9650\u5236\u53d6\u503c\u8303\u56f4\uff1a<\/strong> \u5b57\u9762\u91cf\u7c7b\u578b\u548c\u679a\u4e3e\u90fd\u7528\u4e8e\u9650\u5236\u53d8\u91cf\u7684\u53d6\u503c\u8303\u56f4\uff0c\u786e\u4fdd\u53d8\u91cf\u53ea\u80fd\u53d6\u7279\u5b9a\u7684\u503c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u589e\u5f3a\u53ef\u8bfb\u6027\uff1a<\/strong> \u5b83\u4eec\u90fd\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u56e0\u4e3a\u5b83\u4eec\u5145\u5f53\u4e86\u4e00\u79cd\u8bed\u4e49\u5316\u7684\u6807\u8bb0\uff0c\u8bf4\u660e\u53d8\u91cf\u7684\u542b\u4e49\u3002<\/p>\n<\/li>\n<\/ol>\n<h3><a id=\"%E4%B8%8D%E5%90%8C%E4%B9%8B%E5%A4%84%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e0d\u540c\u4e4b\u5904\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u65b9\u5f0f\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u5b57\u9762\u91cf\u7c7b\u578b\uff1a<\/strong> \u5b57\u9762\u91cf\u7c7b\u578b\u4f7f\u7528\u5b57\u9762\u91cf\u76f4\u63a5\u5b9a\u4e49\u53d8\u91cf\u7684\u53d6\u503c\uff0c\u4f8b\u5982 <code>'success' | 'failure'<\/code>\u3002<\/li>\n<li><strong>\u679a\u4e3e\uff1a<\/strong> \u679a\u4e3e\u4f7f\u7528\u6807\u8bc6\u7b26\uff08identifier\uff09\u6765\u5b9a\u4e49\u53d8\u91cf\u7684\u53d6\u503c\uff0c\u4f8b\u5982 <code>enum Status { Success, Failure }<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7c7b\u578b\u5f62\u5f0f\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u5b57\u9762\u91cf\u7c7b\u578b\uff1a<\/strong> \u5b57\u9762\u91cf\u7c7b\u578b\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u53ef\u4ee5\u4e0e\u5176\u4ed6\u7c7b\u578b\u7ec4\u5408\u4f7f\u7528\uff0c\u5982\u8054\u5408\u7c7b\u578b\uff0c\u4ea4\u53c9\u7c7b\u578b\u7b49\u3002<\/li>\n<li><strong>\u679a\u4e3e\uff1a<\/strong> \u679a\u4e3e\u672c\u8eab\u5c31\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u679a\u4e3e\u6210\u5458\u662f\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u4f46\u679a\u4e3e\u7c7b\u578b\u672c\u8eab\u4e5f\u662f\u4e00\u79cd\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u53ef\u6269\u5c55\u6027\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u5b57\u9762\u91cf\u7c7b\u578b\uff1a<\/strong> \u5b57\u9762\u91cf\u7c7b\u578b\u4e0d\u6613\u6269\u5c55\u3002\u4e00\u65e6\u5b9a\u4e49\u4e86\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u5c31\u53ea\u80fd\u4f7f\u7528\u5176\u4e2d\u7684\u503c\u3002<\/li>\n<li><strong>\u679a\u4e3e\uff1a<\/strong> \u679a\u4e3e\u662f\u53ef\u4ee5\u6269\u5c55\u7684\u3002\u4f60\u53ef\u4ee5\u5411\u679a\u4e3e\u4e2d\u6dfb\u52a0\u65b0\u7684\u6210\u5458\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\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<h4><a id=\"%E4%BD%BF%E7%94%A8%E5%AD%97%E9%9D%A2%E9%87%8F%E7%B1%BB%E5%9E%8B%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u5b57\u9762\u91cf\u7c7b\u578b\uff1a<\/h4>\n<pre><code class=\"language-typescript\">type Status = 'success' | 'failure';\n\nfunction logStatus(status: Status) {\n  console.log(`Status: ${status}`);\n}\n\nlogStatus('success'); \/\/ \u5408\u6cd5\nlogStatus('failure'); \/\/ \u5408\u6cd5\n\/\/ logStatus('error'); \/\/ \u4e0d\u5408\u6cd5\uff0c\u53ea\u80fd\u662f 'success' \u6216 'failure'\n<\/code><\/pre>\n<h4><a id=\"%E4%BD%BF%E7%94%A8%E6%9E%9A%E4%B8%BE%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u679a\u4e3e\uff1a<\/h4>\n<pre><code class=\"language-typescript\">enum Status {\n  Success,\n  Failure,\n}\n\nfunction logStatus(status: Status) {\n  console.log(`Status: ${status}`);\n}\n\nlogStatus(Status.Success); \/\/ \u5408\u6cd5\nlogStatus(Status.Failure); \/\/ \u5408\u6cd5\n\/\/ logStatus('error'); \/\/ \u4e0d\u5408\u6cd5\uff0c\u53ea\u80fd\u662f Status.Success \u6216 Status.Failure\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Status<\/code> \u88ab\u9650\u5236\u4e3a <code>'success'<\/code> \u6216 <code>'failure'<\/code> \u4e24\u4e2a\u5b57\u9762\u503c\u6216 <code>Status.Success<\/code> \u548c <code>Status.Failure<\/code> \u4e24\u4e2a\u679a\u4e3e\u6210\u5458\u3002\u4f7f\u7528\u5b57\u9762\u91cf\u7c7b\u578b\u548c\u679a\u4e3e\u90fd\u80fd\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\uff0c\u4f46\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002\u901a\u5e38\u6765\u8bf4\uff0c\u5982\u679c\u53d8\u91cf\u7684\u53d6\u503c\u662f\u56fa\u5b9a\u4e14\u4e0d\u53ef\u6269\u5c55\u7684\uff0c\u5b57\u9762\u91cf\u7c7b\u578b\u66f4\u5408\u9002\uff1b\u5982\u679c\u9700\u8981\u4e00\u7ec4\u6709\u5e8f\u7684\u6807\u8bc6\u7b26\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u6269\u5c55\uff0c\u679a\u4e3e\u66f4\u4e3a\u9002\u7528\u3002<\/p>\n<h3><a id=\"%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u5b57\u9762\u91cf\u7c7b\u578b\u662f TypeScript \u7684\u57fa\u672c\u7c7b\u578b\u4e4b\u4e00\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u5f0f\u6765\u5b9a\u4e49\u66f4\u7cbe\u786e\u7684\u7c7b\u578b\u3002<\/li>\n<li>\u5b57\u9762\u91cf\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u5c0f\u578b\u3001\u56fa\u5b9a\u7684\u503c\u96c6\u5408\uff0c\u4f8b\u5982\u72b6\u6001\u7801\u3001\u9519\u8bef\u7801\u6216\u7279\u5b9a\u7684\u914d\u7f6e\u9009\u9879\u3002<\/li>\n<li>\u5b57\u9762\u91cf\u7c7b\u578b\u53ef\u4ee5\u4e0e\u8054\u5408\u7c7b\u578b\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u662f\u591a\u4e2a\u7279\u5b9a\u5b57\u9762\u91cf\u503c\u7684\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528\u5b57\u9762\u91cf\u7c7b\u578b\uff0c\u4f60\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7c7b\u578b\u5b89\u5168\u6027\uff0c\u51cf\u5c11\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5e76\u4f7f\u4ee3\u7801\u7684\u610f\u56fe\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 TypeScript \u4e2d\uff0c\u5b57\u9762\u91cf\u7c7b\u578b\uff08Literal Types\uff09\u662f\u6307\u90a3\u4e9b\u4e0e\u7279\u5b9a\u5b57\u9762\u91cf\u503c\u4e25\u683c\u5bf9\u5e94&#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":[2,20],"tags":[33],"class_list":["post-2017","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","tag-typescript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2017","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=2017"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2017\/revisions"}],"predecessor-version":[{"id":2018,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2017\/revisions\/2018"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}