{"id":52251,"date":"2024-12-03T17:00:09","date_gmt":"2024-12-03T09:00:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52251\/"},"modified":"2024-12-03T17:00:09","modified_gmt":"2024-12-03T09:00:09","slug":"typescript%e5%87%bd%e6%95%b0%e4%bd%93%e4%b8%ad%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%88%a4%e6%96%ad%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52251\/","title":{"rendered":"TypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>TypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aTypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732015124673c7414af582.jpg\" class=\"aligncenter\" title=\"TypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f\u63d2\u56fe\" alt=\"TypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>typescript \u51fd\u6570\u4f53\u4e2d\u5224\u65ad\u53c2\u6570\u7c7b\u578b\u7684\u6280\u5de7<\/h2>\n<p>typescript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u63a5\u53e3\u6765\u8868\u793a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728\u51fd\u6570\u4f53\u4e2d\u5224\u65ad\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u5b9e\u73b0\u7c7b\u578b\u6536\u7a84\uff0c\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<h3>\u4f7f\u7528\u8c13\u8bcd\u51fd\u6570<\/h3>\n<p>\u4e00\u79cd\u65b9\u6cd5\u662f\u7f16\u5199\u8c13\u8bcd\u51fd\u6570\u6765\u624b\u52a8\u68c0\u67e5\u7c7b\u578b\u3002\u8c13\u8bcd\u51fd\u6570\u8fd4\u56de\u7684\u662f value is sometype \u5f62\u5f0f\u7684\u503c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>\/\/ \u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f person\nfunction isperson(o: unknown): o is person {\n  return typeof o.name === 'string' &amp;&amp; typeof o.age === 'number';\n}<\/pre>\n<p>\u5728\u51fd\u6570\u4f53\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u6765\u5224\u65ad\u53c2\u6570\u7684\u7c7b\u578b\uff1a<\/p>\n<pre>function test(some: person | animal) {\n  if (isperson(some)) {\n    \/\/ some \u73b0\u5728\u662f person\n  } else if (isanimal(some)) {\n    \/\/ some \u73b0\u5728\u662f animal\n  }\n}<\/pre>\n<h3>\u4f7f\u7528 io-ts \u5e93<\/h3>\n<p>io-ts \u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u5f3a\u5927\u7684\u5de5\u5177\u6765\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002\u5b83\u53ef\u4ee5\u5b9a\u4e49\u8fd0\u884c\u65f6\u68c0\u67e5\u5de5\u5177\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a typescript \u7c7b\u578b\uff1a<\/p>\n<pre>import * as t from 'io-ts';\n\nconst person = t.type({\n  name: t.string,\n  age: t.number\n});\ntype person = t.typeof&lt;typeof person&gt;;\n\nconst animal = t.type({\n  food: t.string,\n  kind: t.string\n});\ntype animal = t.typeof&lt;typeof animal&gt;;\n\nfunction test(some: person | animal) {\n  if (person.is(some)) {\n    console.log('\u73b0\u5728 some \u662f person', some);\n  } else if (animal.is(some)) {\n    console.log('\u73b0\u5728 some \u662f animal', some);\n  }\n}<\/pre>\n<h3>\u4f7f\u7528 class<\/h3>\n<p>\u5728 typescript \u4e2d\uff0cclass \u65e2\u662f\u7c7b\u578b\u4e5f\u662f\u503c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 class \u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528 instanceof \u6765\u68c0\u67e5\u539f\u578b\u94fe\u4ee5\u5224\u65ad\u7c7b\u578b\uff1a<\/p>\n<pre>class Person {\n  name: string;\n  age: number;\n}\n\nclass Animal {\n  food: string;\n  kind: string;\n}\n\nfunction test(some: Person | Animal) {\n  if (some instanceof Person) {\n    console.log('\u73b0\u5728 some \u662f Person', some);\n  } else if (some instanceof Animal) {\n    console.log('\u73b0\u5728 some \u662f Animal', some);\n  }\n}<\/pre>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728 typescript \u51fd\u6570\u4f53\u4e2d\u5224\u65ad\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u5b9e\u73b0\u7c7b\u578b\u6536\u7a84\u5e76\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aTypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Python \u4fee\u9970\u5668\u4e2d\uff1a\u4f55\u65f6\u9700\u8981\u663e\u5f0f\u8c03\u7528\u88ab\u4fee\u9970\u51fd\u6570\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300aTypeScript\u51fd\u6570\u4f53\u4e2d\u5982\u4f55\u9ad8\u6548\u5224\u65ad\u53c2\u6570\u7c7b\u578b\uff1f\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 typescript \u51fd\u6570\u4f53\u4e2d\u5224\u65ad\u53c2\u6570\u7c7b\u578b\u7684\u6280\u5de7 typescript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u63a5\u53e3\u6765\u8868\u793a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728\u51fd\u6570\u4f53\u4e2d\u5224\u65ad\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u5b9e\u73b0\u7c7b\u578b\u6536\u7a84\uff0c\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u7c7b\u578b\u68c0\u67e5\u3002 \u4f7f\u7528\u8c13\u8bcd\u51fd\u6570 \u4e00\u79cd\u65b9\u6cd5\u662f\u7f16\u5199\u8c13\u8bcd\u51fd\u6570\u6765\u624b\u52a8\u68c0\u67e5\u7c7b\u578b\u3002\u8c13\u8bcd\u51fd\u6570\u8fd4\u56de\u7684\u662f value is sometype \u5f62\u5f0f\u7684\u503c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\u51fd\u6570\uff1a \/\/ \u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f person function isperson(o: unknown): o is person { return typeof o.name === &#8216;string&#8217; &amp;&amp; typeof o.age === &#8216;number&#8217;; } \u5728\u51fd\u6570\u4f53\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u6765\u5224\u65ad\u53c2\u6570\u7684\u7c7b\u578b\uff1a function test(some: person | animal) { if (isperson(some)) { \/\/ some \u73b0\u5728\u662f person } else if (isanimal(some)) { \/\/ some \u73b0\u5728\u662f [&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-52251","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52251","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=52251"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52251\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}