{"id":53068,"date":"2024-12-03T15:58:43","date_gmt":"2024-12-03T07:58:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53068\/"},"modified":"2024-12-03T15:58:43","modified_gmt":"2024-12-03T07:58:43","slug":"typescript%e5%87%bd%e6%95%b0%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e5%9c%b0%e5%88%a4%e5%ae%9a%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53068\/","title":{"rendered":"TypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>TypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aTypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\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\/20241126\/1732596909674554adadad5.jpg\" class=\"aligncenter\" title=\"TypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f\u63d2\u56fe\" alt=\"TypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 typescript \u4e2d\u51fd\u6570\u4f53\u4e2d\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\u7684\u5999\u62db<\/strong><\/p>\n<p>\u6587\u4e2d\u63d0\u51fa\u4e86\u4e00\u79cd\u9700\u6c42\uff0c\u9700\u8981\u5728\u4e00\u4e2a typescript \u51fd\u6570\u4e2d\u5224\u5b9a\u53c2\u6570\u7684\u7c7b\u578b\u3002\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5206\u522b\u5bf9\u5e94 person \u548c animal \u63a5\u53e3\u3002\u76ee\u6807\u662f\u5728\u51fd\u6570\u4f53\u4e2d\u8bc6\u522b\u53c2\u6570\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u5e76\u4f9d\u636e\u4e0d\u540c\u7c7b\u578b\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002<\/p>\n<p><strong>\u7c7b\u578b\u5224\u5b9a<\/strong><\/p>\n<p><strong>1. \u624b\u52a8\u7f16\u5199\u8c13\u8bcd\u51fd\u6570<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 javascript \u7684 in \u8fd0\u7b97\u7b26\u6765\u68c0\u67e5\u5bf9\u8c61\u7684\u5c5e\u6027\u662f\u5426\u5b58\u5728\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7f16\u5199\u624b\u52a8\u8c13\u8bcd\u51fd\u6570\u6765\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>function isperson(o: unknown): o is person {\n    ... \/\/ \u68c0\u67e5\u5c5e\u6027\u662f\u5426\u5b58\u5728\u548c\u7c7b\u578b\u7684\u903b\u8f91\n}\n\nfunction isanimal(o: unknown): o is animal {\n    ... \/\/ \u68c0\u67e5\u5c5e\u6027\u662f\u5426\u5b58\u5728\u548c\u7c7b\u578b\u7684\u903b\u8f91\n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5e93 io-ts<\/strong><\/p>\n<p>io-ts \u662f\u4e00\u4e2a\u6d41\u884c\u7684 typescript \u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4fbf\u6377\u51fd\u6570\u6765\u521b\u5efa\u7c7b\u578b\u7c7b\u578b\u5224\u5b9a\u5de5\u5177\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import * as t from 'io-ts'\n\nconst user = t.type({\n    name: t.string,\n    age: t.number\n})\n\nconst animal = t.type({\n    food: t.string,\n    kind: t.string\n})\n\nfunction test(some: user | animal) {\n    if (user.is(some)) {\n        ... \/\/ some \u7684\u7c7b\u578b\u5df2\u6536\u7a84\u4e3a user\n    }\n\n    if (animal.is(some)) {\n        ... \/\/ some \u7684\u7c7b\u578b\u5df2\u6536\u7a84\u4e3a animal\n    }\n}<\/pre>\n<p><strong>3. \u4f7f\u7528 class \u5b9e\u4f8b\u5e76\u7ed3\u5408 instanceof<\/strong><\/p>\n<p>typescript \u7684 class \u65e2\u662f\u7c7b\u578b\u53c8\u662f\u503c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u521b\u5efa class \u5b9e\u4f8b\u5e76\u4f7f\u7528 instanceof \u8fd0\u7b97\u7b26\u6765\u68c0\u67e5\u5bf9\u8c61\u7684\u539f\u578b\u94fe\uff0c\u4ece\u800c\u5224\u65ad\u5176\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class Person {\n    ... \/\/ \u5c5e\u6027\u548c\u6784\u9020\u51fd\u6570\u903b\u8f91\n}\n\nclass Animal {\n    ... \/\/ \u5c5e\u6027\u548c\u6784\u9020\u51fd\u6570\u903b\u8f91\n}\n\nfunction test(some: Person | Animal) {\n    if (some instanceof Person) {\n        ... \/\/ some \u7684\u7c7b\u578b\u5df2\u6536\u7a84\u4e3a Person\n    }\n\n    if (some instanceof Animal) {\n        ... \/\/ some \u7684\u7c7b\u578b\u5df2\u6536\u7a84\u4e3a Animal\n    }\n}<\/pre>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u5c55\u793a\u4e86\u5728 typescript \u51fd\u6570\u4f53\u4e2d\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\u7684\u4e09\u79cd\u5e38\u89c1\u6280\u672f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aTypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff1a\u7ec4\u5408\u4e0e\u805a\u5408\u5982\u4f55\u901a\u8fc7\u7c7b\u5c5e\u6027\u5b9e\u73b0\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f \u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aTypeScript\u51fd\u6570\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u5728 typescript \u4e2d\u51fd\u6570\u4f53\u4e2d\u5224\u5b9a\u53c2\u6570\u7c7b\u578b\u7684\u5999\u62db \u6587\u4e2d\u63d0\u51fa\u4e86\u4e00\u79cd\u9700\u6c42\uff0c\u9700\u8981\u5728\u4e00\u4e2a typescript \u51fd\u6570\u4e2d\u5224\u5b9a\u53c2\u6570\u7684\u7c7b\u578b\u3002\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570\u5904\u7406\u4e24\u79cd\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5206\u522b\u5bf9\u5e94 person \u548c animal \u63a5\u53e3\u3002\u76ee\u6807\u662f\u5728\u51fd\u6570\u4f53\u4e2d\u8bc6\u522b\u53c2\u6570\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u5e76\u4f9d\u636e\u4e0d\u540c\u7c7b\u578b\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002 \u7c7b\u578b\u5224\u5b9a 1. \u624b\u52a8\u7f16\u5199\u8c13\u8bcd\u51fd\u6570 \u53ef\u4ee5\u4f7f\u7528 javascript \u7684 in \u8fd0\u7b97\u7b26\u6765\u68c0\u67e5\u5bf9\u8c61\u7684\u5c5e\u6027\u662f\u5426\u5b58\u5728\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7f16\u5199\u624b\u52a8\u8c13\u8bcd\u51fd\u6570\u6765\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7c7b\u578b\u3002\u4f8b\u5982\uff1a function isperson(o: unknown): o is person { &#8230; \/\/ \u68c0\u67e5\u5c5e\u6027\u662f\u5426\u5b58\u5728\u548c\u7c7b\u578b\u7684\u903b\u8f91 } function isanimal(o: unknown): o is animal { &#8230; \/\/ \u68c0\u67e5\u5c5e\u6027\u662f\u5426\u5b58\u5728\u548c\u7c7b\u578b\u7684\u903b\u8f91 } 2. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5e93 io-ts io-ts \u662f\u4e00\u4e2a\u6d41\u884c\u7684 typescript \u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4fbf\u6377\u51fd\u6570\u6765\u521b\u5efa\u7c7b\u578b\u7c7b\u578b\u5224\u5b9a\u5de5\u5177\u3002\u4f8b\u5982\uff1a import * as t from &#8216;io-ts&#8217; [&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-53068","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53068","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=53068"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53068\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}