{"id":36924,"date":"2025-05-13T01:03:31","date_gmt":"2025-05-12T17:03:31","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/36924.html"},"modified":"2025-05-13T01:03:31","modified_gmt":"2025-05-12T17:03:31","slug":"typescript-interface-vs-type-%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/36924.html","title":{"rendered":"TypeScript interface vs type \u5bf9\u6bd4"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u524d\u8a00<\/li>\n<li>TypeScript &#096;interface&#096; vs &#096;type&#096; \u5bf9\u6bd4<\/li>\n<li>\n<ul>\n<li>1. \u8bed\u6cd5\u5dee\u5f02<\/li>\n<li>2. \u4f7f\u7528\u573a\u666f\u63a8\u8350<\/li>\n<li>3. \u6269\u5c55\u6027\u4e0e\u517c\u5bb9\u6027<\/li>\n<li>4. \u7f16\u8bd1\u540e\u884c\u4e3a<\/li>\n<li>5. \u4e0e\u7b2c\u4e09\u65b9\u7c7b\u578b\u7cfb\u7edf\u96c6\u6210\u5efa\u8bae<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>\u524d\u8a00<\/h2>\n<p>TypeScript \u4e2d\u7684 interface \u4e0e type \u505a\u4e00\u4e2a\u6df1\u5165\u5bf9\u6bd4&#xff0c;\u5305\u62ec\u8bed\u6cd5\u7279\u6027\u3001\u4f7f\u7528\u573a\u666f\u3001\u517c\u5bb9\u6027\u4e0e\u6269\u5c55\u6027&#xff0c;\u4ee5\u53ca\u5e95\u5c42\u884c\u4e3a\u5dee\u5f02\u3002<\/p>\n<h2>TypeScript interface vs type \u5bf9\u6bd4<\/h2>\n<p>\u5728 TypeScript \u4e2d&#xff0c;\u53ef\u4ee5\u7528 interface&#xff08;\u63a5\u53e3&#xff09;\u6216 type&#xff08;\u7c7b\u578b\u522b\u540d&#xff09;\u4e24\u79cd\u65b9\u5f0f\u5b9a\u4e49\u7c7b\u578b\u3002\u5b83\u4eec\u6709\u8bf8\u591a\u76f8\u4f3c\u4e4b\u5904&#xff0c;\u4f46\u5728\u8bed\u6cd5\u3001\u4f7f\u7528\u573a\u666f\u3001\u6269\u5c55\u6027\u3001\u7f16\u8bd1\u884c\u4e3a\u548c\u4e0e\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210\u7b49\u65b9\u9762\u5b58\u5728\u5dee\u5f02\u3002\u4e0b\u9762\u4ece\u8fd9\u4e94\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u8be6\u7ec6\u5bf9\u6bd4&#xff0c;\u5e76\u7ed3\u5408\u4ee3\u7801\u793a\u4f8b\u3001\u8868\u683c\u8bf4\u660e\u5dee\u5f02\u70b9&#xff0c;\u6700\u540e\u603b\u7ed3\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h3>1. \u8bed\u6cd5\u5dee\u5f02<\/h3>\n<p>\u5b9a\u4e49\u8bed\u6cd5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u63a5\u53e3 (interface) \u5b9a\u4e49&#xff1a; \u4f7f\u7528 interface Name { &#8230; } \u58f0\u660e\u5bf9\u8c61\u7684\u7ed3\u6784\u6216\u51fd\u6570\u7684\u7b7e\u540d\u3002\u4f8b\u5982\u5b9a\u4e49\u4e00\u4e2a\u70b9\u5750\u6807\u63a5\u53e3&#xff1a;<\/p>\n<p> <span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Point<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  x<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  y<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u63a5\u53e3\u4e5f\u53ef\u7528\u4e8e\u5b9a\u4e49\u51fd\u6570\u7c7b\u578b&#xff0c;\u5199\u6cd5\u662f\u5728\u63a5\u53e3\u5185\u90e8\u76f4\u63a5\u58f0\u660e\u8c03\u7528\u7b7e\u540d&#xff0c;\u5982&#xff1a;<\/p>\n<p> <span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">SetPoint<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token punctuation\">(<\/span>x<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">,<\/span> y<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span> <span class=\"token keyword\">void<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e0a\u8ff0 SetPoint \u63a5\u53e3\u8868\u793a\u4e00\u4e2a\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u4e14\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u7c7b\u578b\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u578b\u522b\u540d (type) \u5b9a\u4e49&#xff1a; \u4f7f\u7528 type Name &#061; &#8230; \u7ed9\u4efb\u4f55\u7c7b\u578b\u8d77\u522b\u540d\u3002\u5b83\u53ef\u4ee5\u662f\u5bf9\u8c61\u3001\u57fa\u672c\u7c7b\u578b\u3001\u8054\u5408\/\u4ea4\u53c9\u7c7b\u578b\u7b49\u3002\u4f8b\u5982\u540c\u6837\u5b9a\u4e49\u70b9\u5750\u6807\u7c7b\u578b&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Point<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  x<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  y<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5b9a\u4e49\u51fd\u6570\u7c7b\u578b\u522b\u540d&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">SetPoint<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>x<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">,<\/span> y<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token keyword\">void<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u53ef\u4ee5\u770b\u51fa&#xff0c;\u7528 type \u5b9a\u4e49\u5bf9\u8c61\u7c7b\u578b\u65f6\u9700\u8981\u4f7f\u7528 &#061; \u53f7\u8d4b\u503c&#xff0c;\u5e76\u5728\u5bf9\u8c61\u5b57\u9762\u91cf\u540e\u52a0\u5206\u53f7\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7ee7\u627f\u4e0e\u7ec4\u5408&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u63a5\u53e3\u7ee7\u627f (extends)&#xff1a; \u63a5\u53e3\u53ef\u4ee5\u901a\u8fc7 extends \u7ee7\u627f\u53e6\u4e00\u4e2a\u63a5\u53e3&#xff0c;\u4ece\u800c\u6269\u5c55\u5176\u5c5e\u6027&#xff1a;<\/p>\n<p> <span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Animal<\/span> <span class=\"token punctuation\">{<\/span> name<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Bear<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">Animal<\/span> <span class=\"token punctuation\">{<\/span> honey<span class=\"token operator\">:<\/span> <span class=\"token builtin\">boolean<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ Bear \u62e5\u6709 name \u548c honey \u4e24\u4e2a\u5c5e\u6027<\/span><\/p>\n<p>\u5982\u679c\u8981\u7ee7\u627f\u591a\u4e2a\u63a5\u53e3&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u63a5\u53e3\u540d\u79f0\u3002\u4f8b\u5982 interface C extends A, B { &#8230; }\u3002\u63a5\u53e3\u53ea\u80fd\u7ee7\u627f\u63a5\u53e3\u6216\u7c7b\u578b\u522b\u540d\u6240\u8868\u793a\u7684\u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u578b\u522b\u540d\u4ea4\u53c9 (&amp;)&#xff1a; \u7c7b\u578b\u522b\u540d\u672c\u8eab\u4e0d\u80fd\u4f7f\u7528 extends \u5173\u952e\u5b57&#xff0c;\u4f46\u53ef\u4ee5\u901a\u8fc7\u4ea4\u53c9\u7c7b\u578b&#xff08;intersection &amp;&#xff09;\u6765\u7ec4\u5408&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Animal<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> name<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Bear<\/span> <span class=\"token operator\">&#061;<\/span> Animal <span class=\"token operator\">&amp;<\/span> <span class=\"token punctuation\">{<\/span> honey<span class=\"token operator\">:<\/span> <span class=\"token builtin\">boolean<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ Bear \u62e5\u6709 name \u548c honey \u4e24\u4e2a\u5c5e\u6027<\/span><\/p>\n<p>\u4ea4\u53c9\u7c7b\u578b\u5c06\u591a\u4e2a\u7c7b\u578b\u5408\u5e76\u4e3a\u4e00\u4e2a\u7c7b\u578b&#xff0c;\u8fbe\u5230\u7c7b\u4f3c\u7ee7\u627f\u7684\u6548\u679c\u3002\u9700\u8981\u6ce8\u610f&#xff0c;\u4ea4\u53c9\u5408\u5e76\u65f6\u5982\u679c\u5c5e\u6027\u51b2\u7a81&#xff0c;TypeScript \u4f1a\u63a8\u5bfc\u51fa never&#xff08;\u5373\u4e0d\u5b58\u5728\u53ef\u6ee1\u8db3\u53cc\u65b9\u8981\u6c42\u7684\u7c7b\u578b&#xff09;\u3002\u800c\u63a5\u53e3\u7ee7\u627f\u5728\u9047\u5230\u5c5e\u6027\u7c7b\u578b\u51b2\u7a81\u65f6\u4f1a\u76f4\u63a5\u62a5\u9519&#xff0c;\u5f3a\u5236\u5f00\u53d1\u8005\u5904\u7406\u51b2\u7a81&#xff0c;\u8fd9\u901a\u5e38\u662f\u66f4\u5408\u7406\u7684\u884c\u4e3a\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u58f0\u660e\u5408\u5e76&#xff1a; \u63a5\u53e3\u662f\u5f00\u653e\u7684&#xff0c;\u5141\u8bb8\u591a\u4e2a\u540c\u540d\u63a5\u53e3\u58f0\u660e\u4f1a\u88ab\u81ea\u52a8\u5408\u5e76\u3002\u7c7b\u578b\u522b\u540d\u662f\u5c01\u95ed\u7684&#xff0c;\u4e0d\u80fd\u91cd\u590d\u58f0\u660e\u540c\u540d\u7684\u7c7b\u578b\u522b\u540d\u3002\u6bd4\u5982&#xff1a;<\/p>\n<p><span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Kitten<\/span> <span class=\"token punctuation\">{<\/span> purrs<span class=\"token operator\">:<\/span> <span class=\"token builtin\">boolean<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Kitten<\/span> <span class=\"token punctuation\">{<\/span> color<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4e24\u6b21\u58f0\u660e\u4f1a\u5408\u5e76\u4e3a\u4e00\u4e2a Kitten \u63a5\u53e3&#xff0c;\u62e5\u6709 purrs \u548c color \u4e24\u4e2a\u5c5e\u6027<\/span><\/p>\n<p><span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Puppy<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> color<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ type Puppy &#061; { toys: number; }; \/\/ \u9519\u8bef&#xff1a;\u91cd\u590d\u5b9a\u4e49\u540c\u540d\u7c7b\u578b\u522b\u540d<\/span><\/p>\n<p>\u5982\u4e0a&#xff0c;\u7b2c\u4e8c\u6b21\u58f0\u660e\u63a5\u53e3 Kitten \u65f6&#xff0c;\u4f1a\u5c06\u65b0\u5c5e\u6027\u5408\u5e76\u8fdb\u53bb&#xff0c;\u7b49\u6548\u4e8e\u4e00\u4e2a\u63a5\u53e3\u5305\u542b\u6240\u6709\u5c5e\u6027\u3002\u800c\u7c7b\u578b\u522b\u540d Puppy \u65e0\u6cd5\u4e8c\u6b21\u58f0\u660e&#xff0c;\u5426\u5219\u7f16\u8bd1\u9519\u8bef\u3002\u56e0\u6b64\u63a5\u53e3\u53ef\u4ee5\u6269\u5c55\u5df2\u6709\u5b9a\u4e49&#xff08;\u5c24\u5176\u5728\u58f0\u660e\u5168\u5c40\u7c7b\u578b\u65f6\u5f88\u6709\u7528&#xff09;&#xff0c;\u7c7b\u578b\u522b\u540d\u5219\u4e0d\u652f\u6301\u5408\u5e76&#xff08;\u66f4\u4e25\u683c\u5730\u4fdd\u6301\u5355\u4e00\u5b9a\u4e49&#xff09;\u3002<\/p>\n<p>\u5176\u4ed6\u8bed\u6cd5\u5dee\u5f02&#xff1a;<\/p>\n<ul>\n<li>\u63a5\u53e3\u540d\u79f0\u5728\u9519\u8bef\u4fe1\u606f\u4e2d\u4fdd\u7559\u539f\u540d&#xff0c;\u7c7b\u578b\u522b\u540d\u6709\u65f6\u4f1a\u5c55\u5f00\u6210\u5177\u4f53\u5b9a\u4e49\u3002\u4f8b\u5982&#xff0c;\u5f53\u7c7b\u578b\u522b\u540d\u5f88\u590d\u6742\u65f6&#xff0c;TypeScript \u62a5\u9519\u53ef\u80fd\u76f4\u63a5\u663e\u793a\u5c55\u5f00\u540e\u7684\u5177\u4f53\u7c7b\u578b&#xff0c;\u4f7f\u9519\u8bef\u4fe1\u606f\u5197\u957f\u3002\u800c\u63a5\u53e3\u7531\u4e8e\u521b\u5efa\u4e86\u4e00\u4e2a\u663e\u5f0f\u7684\u540d\u5b57&#xff0c;\u9519\u8bef\u63d0\u793a\u901a\u5e38\u4f1a\u76f4\u63a5\u5f15\u7528\u63a5\u53e3\u540d&#xff0c;\u66f4\u7b80\u6d01\u805a\u7126\u3002\u8fd9\u4e00\u70b9\u5728\u8c03\u8bd5\u5927\u578b\u7c7b\u578b\u65f6\u80fd\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/li>\n<li>\u63a5\u53e3\u4e0d\u80fd\u76f4\u63a5\u8868\u793a\u57fa\u672c\u7c7b\u578b\u522b\u540d\u6216\u8054\u5408\u7c7b\u578b&#xff0c;\u800c\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u4e3a\u51e0\u4e4e\u4efb\u4f55\u7c7b\u578b\u547d\u540d&#xff08;\u8be6\u89c1\u4e0b\u6587\u573a\u666f\u90e8\u5206&#xff09;\u3002\u63a5\u53e3\u53ea\u80fd\u7528\u4e8e\u5bf9\u8c61\u5f62\u72b6\u6216\u51fd\u6570\u7684\u7ed3\u6784\u63cf\u8ff0\u3002\u76f8\u5bf9\u5730&#xff0c;type \u53ef\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\u3001\u5143\u7ec4\u3001\u8054\u5408\u3001\u6761\u4ef6\u7c7b\u578b\u7b49\u66f4\u591a\u573a\u666f&#xff0c;\u8fd9\u5c5e\u4e8e\u8bed\u6cd5\u4e0a\u7684\u7075\u6d3b\u6027\u5dee\u5f02\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u8868\u683c\u603b\u7ed3\u4e86\u4e3b\u8981\u8bed\u6cd5\u7279\u6027\u652f\u6301\u60c5\u51b5&#xff1a;<\/p>\n<table>\n<tr>\u7279\u6027interface \u652f\u6301\u60c5\u51b5type \u652f\u6301\u60c5\u51b5<\/tr>\n<tbody>\n<tr>\n<td>\u5b9a\u4e49\u5bf9\u8c61\u7ed3\u6784<\/td>\n<td>\u662f&#xff1a;interface X { &#8230; }<\/td>\n<td>\u662f&#xff1a;type X &#061; { &#8230; }<\/td>\n<\/tr>\n<tr>\n<td>\u5b9a\u4e49\u51fd\u6570\u7c7b\u578b<\/td>\n<td>\u662f&#xff1a;\u8c03\u7528\u7b7e\u540d\u8bed\u6cd5\u5b9a\u4e49<\/td>\n<td>\u662f&#xff1a;\u7bad\u5934\u51fd\u6570\u8bed\u6cd5\u5b9a\u4e49<\/td>\n<\/tr>\n<tr>\n<td>\u57fa\u672c\u7c7b\u578b\u8d77\u522b\u540d<\/td>\n<td>\u5426&#xff08;\u63a5\u53e3\u4e0d\u80fd\u76f4\u63a5\u8868\u793a\u57fa\u672c\u7c7b\u578b&#xff09;<\/td>\n<td>\u662f&#xff1a;\u5982 type Name &#061; string<\/td>\n<\/tr>\n<tr>\n<td>\u8054\u5408\u7c7b\u578b&#xff08;\u591a\u4e2a\u7c7b\u578b\u4e4b\u4e00&#xff09;<\/td>\n<td>\u5426&#xff08;\u63a5\u53e3\u65e0\u6cd5\u76f4\u63a5\u4e3a\u8054\u5408\u547d\u540d&#xff09;<\/td>\n<td>\u662f&#xff1a;\u5982 type U &#061; A | B<\/td>\n<\/tr>\n<tr>\n<td>\u5143\u7ec4\u7c7b\u578b<\/td>\n<td>\u5426&#xff08;\u63a5\u53e3\u65e0\u6cd5\u76f4\u63a5\u8868\u793a\u5143\u7ec4&#xff09;<\/td>\n<td>\u662f&#xff1a;\u5982 type T &#061; [number, string]<\/td>\n<\/tr>\n<tr>\n<td>\u6cdb\u578b\u53c2\u6570<\/td>\n<td>\u662f&#xff1a;\u63a5\u53e3\u58f0\u660e\u53ef\u6cdb\u578b &lt;T&gt;<\/td>\n<td>\u662f&#xff1a;\u7c7b\u578b\u522b\u540d\u53ef\u6cdb\u578b &lt;T&gt;<\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5c55\/\u7ee7\u627f\u5176\u4ed6\u7c7b\u578b<\/td>\n<td>\u662f&#xff1a;extends \u5173\u952e\u5b57<\/td>\n<td>\u662f&#xff1a;\u4ea4\u53c9\u7c7b\u578b &amp;<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u6b21\u58f0\u660e\u5408\u5e76<\/td>\n<td>\u662f&#xff1a;\u81ea\u52a8\u5408\u5e76\u5c5e\u6027<\/td>\n<td>\u5426&#xff1a;\u91cd\u590d\u58f0\u660e\u4f1a\u62a5\u9519<\/td>\n<\/tr>\n<tr>\n<td>\u7c7b\u5b9e\u73b0 (implements)<\/td>\n<td>\u662f&#xff1a;\u7c7b\u53ef\u5b9e\u73b0\u63a5\u53e3<\/td>\n<td>\u662f&#xff1a;\u7c7b\u53ef\u5b9e\u73b0\u5bf9\u8c61\u7c7b\u578b\u522b\u540d&#xff08;\u975e\u8054\u5408&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u63a5\u53e3\/\u7c7b\u578b\u53ef\u76f8\u4e92\u6269\u5c55<\/td>\n<td>\u63a5\u53e3\u53ef extends \u7c7b\u578b\u522b\u540d<\/td>\n<td>\u7c7b\u578b\u522b\u540d\u53ef\u901a\u8fc7 &amp; \u5305\u542b\u63a5\u53e3<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u8bef\u4fe1\u606f\u4fdd\u7559\u7c7b\u578b\u540d<\/td>\n<td>\u662f<\/td>\n<td>\u4e00\u822c\u5426&#xff08;\u5e38\u5185\u8054\u5c55\u5f00&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u652f\u6301\u6620\u5c04\u7c7b\u578b\u3001\u6761\u4ef6\u7c7b\u578b\u7b49\u590d\u6742\u7c7b\u578b<\/td>\n<td>\u5426&#xff08;\u63a5\u53e3\u672c\u8eab\u4e0d\u76f4\u63a5\u652f\u6301&#xff09;<\/td>\n<td>\u662f&#xff08;\u53ef\u5b9a\u4e49\u5982 type X&lt;T&gt; &#061; T extends &#8230;&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2. \u4f7f\u7528\u573a\u666f\u63a8\u8350<\/h3>\n<p>\u6839\u636e\u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u5404\u81ea\u7684\u7279\u6027&#xff0c;\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u6709\u63a8\u8350\u7684\u4f7f\u7528\u65b9\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7ec4\u4ef6 Props \u5b9a\u4e49&#xff1a; \u5728 React \u548c Vue \u7b49\u6846\u67b6\u4e2d\u5b9a\u4e49\u7ec4\u4ef6\u7684 props \u7c7b\u578b\u65f6&#xff0c;\u63a5\u53e3\u901a\u5e38\u662f\u9996\u9009\u3002\u63a5\u53e3\u66f4\u7b26\u5408\u201c\u63cf\u8ff0\u5bf9\u8c61\u7ed3\u6784\u201d\u7684\u8bed\u4e49&#xff0c;\u800c\u4e14\u53ef\u4ee5\u62d3\u5c55&#xff08;\u4f8b\u5982\u5728\u9ad8\u9636\u7ec4\u4ef6\u6216\u6269\u5c55 HTML \u539f\u751f\u5c5e\u6027\u65f6\u66f4\u65b9\u4fbf&#xff09;\u3002React \u793e\u533a\u4e2d&#xff0c;\u4e0d\u5c11\u4eba\u504f\u597d\u7528\u63a5\u53e3\u6765\u5b9a\u4e49 Props \u548c State&#xff0c;\u56e0\u4e3a\u63a5\u53e3\u652f\u6301\u6269\u5c55\u548c\u5408\u5e76&#xff0c;\u7c7b\u578b\u68c0\u67e5\u9519\u8bef\u4fe1\u606f\u4e5f\u66f4\u6e05\u6670\u3002\u4f8b\u5982\u5728 React \u4e2d&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ \u7528\u63a5\u53e3\u5b9a\u4e49\u7ec4\u4ef6Props<\/span><br \/>\n<span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">ButtonProps<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">React<\/span><span class=\"token punctuation\">.<\/span>ButtonHTMLAttributes<span class=\"token operator\">&lt;<\/span>HTMLButtonElement<span class=\"token operator\">&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  label<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">Button<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span> label<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8230;<\/span>rest <span class=\"token punctuation\">}<\/span><span class=\"token operator\">:<\/span> ButtonProps<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&lt;<\/span>button <span class=\"token punctuation\">{<\/span><span class=\"token operator\">&#8230;<\/span>rest<span class=\"token punctuation\">}<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">{<\/span>label<span class=\"token punctuation\">}<\/span><span class=\"token operator\">&lt;<\/span><span class=\"token operator\">\/<\/span>button<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e0a\u4f8b\u4e2d\u63a5\u53e3\u901a\u8fc7 extends \u8f7b\u677e\u7ee7\u627f\u4e86\u539f\u751f\u6309\u94ae\u5c5e\u6027\u7c7b\u578b&#xff0c;\u9644\u52a0\u4e86\u81ea\u5b9a\u4e49\u7684 label \u5c5e\u6027\u3002\u5982\u679c\u7528\u7c7b\u578b\u522b\u540d&#xff0c;\u5219\u9700\u8981\u4ea4\u53c9\u7c7b\u578b &amp; \u6765\u7ec4\u5408&#xff0c;\u867d\u529f\u80fd\u7b49\u6548\u4f46\u5728\u5927\u578b\u9879\u76ee\u4e2d\u53ef\u80fd\u5f15\u5165\u6027\u80fd\u95ee\u9898&#xff08;\u4e0b\u6587\u8be6\u8ff0&#xff09;\u3002\u5b9e\u8df5\u7ecf\u9a8c\u8868\u660e&#xff0c;\u5f53\u9700\u8981\u5c06\u591a\u4e2a\u6765\u6e90\u7684Props\u7ec4\u5408\u65f6&#xff0c;\u4f7f\u7528\u63a5\u53e3\u7684 extends \u5bf9\u7f16\u8bd1\u5668\u66f4\u53cb\u597d\u3001\u66f4\u9ad8\u6548\u3002 \u6ce8&#xff1a; \u5728\u7b80\u5355\u573a\u666f\u4e0b&#xff08;\u6bd4\u5982\u65e0\u9700\u6269\u5c55\u7684\u5c40\u90e8Props)&#xff0c;\u4f7f\u7528 type \u5b9a\u4e49\u4e5f\u5b8c\u5168\u53ef\u884c\u3002\u5b98\u65b9 React \u6587\u6863\u6307\u51fa&#xff0c;Props \u7c7b\u578b\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u7528 type \u6216 interface \u5b9a\u4e49\u5747\u53ef\u3002\u56e0\u6b64\u56e2\u961f\u53ef\u6839\u636e\u4e60\u60ef\u9009\u4e00\u79cd&#xff0c;\u4f46\u5e94\u4fdd\u6301\u4e00\u81f4\u548c\u6e05\u6670\u3002<\/p>\n<\/li>\n<li>\n<p>\u8054\u5408\u7c7b\u578b (Union Types)&#xff1a; \u5982\u679c\u4e00\u4e2a\u7c7b\u578b\u9700\u8981\u8868\u793a\u201c\u51e0\u79cd\u7c7b\u578b\u4e4b\u4e00\u201d&#xff0c;\u53ea\u80fd\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u6765\u5b9a\u4e49\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Input<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> tag<span class=\"token operator\">:<\/span> <span class=\"token string\">&#039;text&#039;<\/span><span class=\"token punctuation\">;<\/span> text<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n          <span class=\"token operator\">|<\/span> <span class=\"token punctuation\">{<\/span> tag<span class=\"token operator\">:<\/span> <span class=\"token string\">&#039;checkbox&#039;<\/span><span class=\"token punctuation\">;<\/span> checked<span class=\"token operator\">:<\/span> <span class=\"token builtin\">boolean<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4ee5\u4e0a\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8054\u5408\u7c7b\u578b Input&#xff0c;\u53ef\u80fd\u662f\u6587\u5b57\u8f93\u5165\u6216\u590d\u9009\u6846\u4e24\u79cd\u5f62\u6001\u3002\u63a5\u53e3\u672c\u8eab\u4e0d\u80fd\u76f4\u63a5\u8868\u8fbe\u8054\u5408\u7c7b\u578b&#xff08;\u65e0\u6cd5\u58f0\u660e interface X &#061; A | B \u8fd9\u79cd\u8bed\u6cd5&#xff09;\u3002\u5c3d\u7ba1\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u63a5\u53e3\u5982 TextInput \u548c CheckboxInput \u518d\u7528\u7c7b\u578b\u522b\u540d\u8054\u5408&#xff0c;\u4f46\u6700\u7ec8\u8fd8\u662f\u901a\u8fc7 type \u6765\u7ec4\u5408\u3002\u5728\u9700\u8981\u5224\u522b\u8054\u5408&#xff08;discriminated union&#xff09;\u6a21\u5f0f\u7684\u573a\u666f&#xff0c;type \u662f\u5fc5\u7136\u9009\u62e9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5de5\u5177\u7c7b\u578b (Utility Types)&#xff1a; TypeScript \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u5de5\u5177\u7c7b\u578b&#xff08;\u5982 Partial&lt;T&gt;, Required&lt;T&gt;, Pick&lt;T, K&gt;, ReturnType&lt;F&gt; \u7b49&#xff09;&#xff0c;\u4ee5\u53ca\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6761\u4ef6\u7c7b\u578b\u548c\u6620\u5c04\u7c7b\u578b\u6765\u53d8\u6362\u7c7b\u578b\u7ed3\u6784\u3002\u8fd9\u7c7b\u9ad8\u7ea7\u7c7b\u578b\u673a\u5236\u53ea\u80fd\u4f7f\u7528 type \u5b9e\u73b0\u3002\u4f8b\u5982&#xff0c;\u7528\u6620\u5c04\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u5168\u90e8\u5c5e\u6027\u53ea\u8bfb\u7684\u5de5\u5177\u7c7b\u578b&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">ReadonlyObj<span class=\"token operator\">&lt;<\/span><span class=\"token constant\">T<\/span><span class=\"token operator\">&gt;<\/span><\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token keyword\">readonly<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token constant\">K<\/span> <span class=\"token keyword\">in<\/span> <span class=\"token keyword\">keyof<\/span> <span class=\"token constant\">T<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">:<\/span> <span class=\"token constant\">T<\/span><span class=\"token punctuation\">[<\/span><span class=\"token constant\">K<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u6216\u6761\u4ef6\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u578b\u7b5b\u9009&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">ElementType<span class=\"token operator\">&lt;<\/span><span class=\"token constant\">T<\/span><span class=\"token operator\">&gt;<\/span><\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token constant\">T<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token punctuation\">(<\/span>infer <span class=\"token constant\">U<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">?<\/span> <span class=\"token constant\">U<\/span> <span class=\"token operator\">:<\/span> <span class=\"token constant\">T<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd9\u4e9b\u573a\u666f\u4e0b\u63a5\u53e3\u90fd\u65e0\u80fd\u4e3a\u529b\u2014\u2014\u63a5\u53e3\u4e0d\u80fd\u5728\u5b9a\u4e49\u65f6\u4f7f\u7528\u6620\u5c04\u6216\u6761\u4ef6\u8bed\u6cd5\u3002\u56e0\u6b64\u5de5\u5177\u7c7b\u578b\u51e0\u4e4e\u90fd\u662f\u7528 type \u5b9a\u4e49\u7684\u3002\u6362\u8a00\u4e4b&#xff0c;\u5f53\u9700\u8981\u5229\u7528\u7c7b\u578b\u7f16\u7a0b\u7684\u9ad8\u7ea7\u7279\u6027&#xff08;\u6761\u4ef6\u5224\u65ad\u3001\u6620\u5c04\u3001\u6a21\u677f\u5b57\u9762\u91cf\u7c7b\u578b\u7b49&#xff09;\u65f6&#xff0c;type \u662f\u552f\u4e00\u9009\u62e9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5185\u7f6e\u5bf9\u8c61\u7c7b\u578b\u6269\u5145&#xff1a; \u5982\u679c\u9700\u8981\u6269\u5c55\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5df2\u6709\u7684\u7c7b\u578b&#xff08;\u4f8b\u5982\u4e3a Window \u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027&#xff0c;\u6216\u4e3a\u7b2c\u4e09\u65b9\u5e93\u7684\u7c7b\u578b\u589e\u52a0\u81ea\u5b9a\u4e49\u5b57\u6bb5&#xff09;&#xff0c;\u63a5\u53e3\u66f4\u9002\u5408\u3002\u56e0\u4e3a\u63a5\u53e3\u652f\u6301\u58f0\u660e\u5408\u5e76&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u91cd\u590d\u58f0\u660e\u540c\u540d\u63a5\u53e3\u6765\u589e\u52a0\u5c5e\u6027\u3002\u5178\u578b\u4f8b\u5b50\u662f\u5728 global.d.ts \u4e2d\u6269\u5c55\u6d4f\u89c8\u5668 Window \u63a5\u53e3&#xff1a;<\/p>\n<p> <span class=\"token comment\">\/\/ \u6269\u5c55\u5168\u5c40 Window \u63a5\u53e3&#xff0c;\u589e\u52a0\u81ea\u5b9a\u4e49\u5c5e\u6027<\/span><br \/>\n<span class=\"token keyword\">declare<\/span> global <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Window<\/span> <span class=\"token punctuation\">{<\/span> myAppVersion<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8fd9\u6837\u5373\u4f7f\u539f\u59cb Window \u5728 lib.dom.d.ts \u4e2d\u5df2\u5b9a\u4e49&#xff0c;\u4e5f\u4f1a\u5408\u5e76\u8fdb\u65b0\u7684\u5b57\u6bb5\u3002\u800c\u7c7b\u578b\u522b\u540d\u4e0d\u652f\u6301\u91cd\u590d\u58f0\u660e&#xff0c;\u56e0\u6b64\u65e0\u6cd5\u7528\u540c\u540d type Window &#061; &#8230; \u6765\u6269\u5c55\u3002\u6b64\u5916&#xff0c;\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u4e5f\u5229\u7528\u63a5\u53e3\u5408\u5e76\u673a\u5236\u63d0\u4f9b\u4e86\u63d2\u4ef6\u5f0f\u7684\u7c7b\u578b\u6269\u5c55\u80fd\u529b\u3002\u6bd4\u5982 Express \u6846\u67b6\u5141\u8bb8\u58f0\u660e declare module &#039;express&#039; { interface Request { &#8230; } } \u6765\u6269\u5c55\u8bf7\u6c42\u5bf9\u8c61\u7c7b\u578b\u3002\u8fd9\u7c7b\u573a\u666f\u5fc5\u987b\u4f7f\u7528\u63a5\u53e3\u7684\u58f0\u660e\u5408\u5e76\u7279\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c01\u95ed\u6570\u636e\u6a21\u578b&#xff1a; \u5982\u679c\u4f60\u6709\u4e9b\u6570\u636e\u7ed3\u6784\u786e\u5b9a\u4e0d\u5e0c\u671b\u88ab\u518d\u6269\u5c55\u4fee\u6539&#xff0c;\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u4f53\u73b0\u8fd9\u4e00\u610f\u56fe\u3002\u7531\u4e8e\u7c7b\u578b\u522b\u540d\u662f\u4e0d\u53ef\u5408\u5e76\u7684&#xff0c;\u201c\u5c01\u95ed\u201d\u4e86\u5b9a\u4e49&#xff0c;\u53ef\u4ee5\u9632\u6b62\u5916\u90e8\u518d\u6dfb\u52a0\u5c5e\u6027\u3002\u867d\u7136 TS \u6ca1\u6709\u771f\u6b63\u7684\u8bbf\u95ee\u63a7\u5236&#xff0c;\u4f46\u901a\u8fc7\u9009\u7528\u7c7b\u578b\u522b\u540d&#xff0c;\u53ef\u4ee5\u5728\u56e2\u961f\u7ea6\u5b9a\u4e0a\u8868\u793a\u6b64\u7c7b\u578b\u4e0d\u5e94\u518d\u88ab\u6269\u5c55\u3002\u6bd4\u5982\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u683c\u5f0f\u7684\u914d\u7f6e\u9879&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Config<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">readonly<\/span> host<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  port<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7528\u63a5\u53e3\u4e5f\u53ef\u4ee5\u505a\u5230\u76f8\u540c\u6548\u679c&#xff0c;\u53ea\u662f\u63a5\u53e3\u59cb\u7ec8\u5141\u8bb8\u88ab\u518d\u6b21\u58f0\u660e\u3002\u6240\u4ee5\u5728\u8ffd\u6c42\u4e25\u683c\u4e0d\u53ef\u53d8\u7684\u5951\u7ea6\u65f6&#xff0c;\u4e00\u4e9b\u5f00\u53d1\u8005\u4f1a\u504f\u597d\u7528 type \u6765\u5b9a\u4e49\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u4ee5\u907f\u514d\u63a5\u53e3\u53ef\u80fd\u88ab\u5408\u5e76\u7684\u201c\u5f00\u653e\u201d\u884c\u4e3a\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7b80\u5355\u6765\u8bf4&#xff0c;\u4e00\u4e2a\u5e38\u89c1\u7684\u7ecf\u9a8c\u6cd5\u5219\u662f&#xff1a;\u201c\u5728\u4e0d\u77e5\u9053\u9009\u54ea\u79cd\u65f6&#xff0c;\u5148\u4f7f\u7528\u63a5\u53e3&#xff0c;\u5f53\u9700\u8981\u7c7b\u578b\u522b\u540d\u72ec\u6709\u7684\u529f\u80fd\u65f6\u518d\u5207\u6362\u201d\u3002TypeScript \u5b98\u65b9\u6587\u6863\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u5efa\u8bae&#xff1a;\u201c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u53ef\u4ee5\u6309\u559c\u597d\u9009\u62e9&#xff0c;TypeScript \u4f1a\u5728\u9700\u8981\u65f6\u544a\u8bc9\u4f60\u7528\u53e6\u4e00\u79cd\u3002\u5982\u679c\u8981\u4e00\u4e2a\u542f\u53d1\u5f0f&#xff0c;\u7528\u63a5\u53e3\u76f4\u5230\u4f60\u9700\u8981\u7528 type \u7684\u7279\u6027\u4e3a\u6b62\u201d\u3002\u56e0\u6b64\u9ed8\u8ba4\u7528\u63a5\u53e3\u63cf\u8ff0\u5bf9\u8c61\u7ed3\u6784&#xff0c;\u5f53\u8981\u5b9a\u4e49\u8054\u5408\u3001\u9ad8\u7ea7\u6620\u5c04\/\u6761\u4ef6\u7c7b\u578b\u6216\u57fa\u672c\u7c7b\u578b\u522b\u540d\u7b49\u63a5\u53e3\u65e0\u6cd5\u8986\u76d6\u7684\u60c5\u51b5&#xff0c;\u5c31\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u3002<\/p>\n<h3>3. \u6269\u5c55\u6027\u4e0e\u517c\u5bb9\u6027<\/h3>\n<p>\u5f00\u653e vs \u5c01\u95ed&#xff1a; \u5982\u524d\u6240\u8ff0&#xff0c;\u63a5\u53e3\u662f\u5f00\u653e\u7684&#xff0c;\u53ef\u4ee5\u591a\u6b21\u58f0\u660e\u540c\u540d\u63a5\u53e3\u5b9e\u73b0\u58f0\u660e\u5408\u5e76\u3002\u8fd9\u79cd\u7279\u6027\u63d0\u4f9b\u4e86\u6781\u9ad8\u7684\u6269\u5c55\u6027&#xff1a;\u4e0d\u540c\u6a21\u5757\u90fd\u53ef\u4ee5\u5411\u540c\u4e00\u4e2a\u63a5\u53e3\u589e\u52a0\u5b57\u6bb5&#xff0c;\u6700\u7ec8\u90fd\u4f1a\u5408\u5e76\u6210\u4e00\u4e2a\u7c7b\u578b\u3002\u4f8b\u5982\u5f88\u591a\u5e93\u7684\u5168\u5c40\u7c7b\u578b\u6269\u5c55\u90fd\u662f\u57fa\u4e8e\u63a5\u53e3\u5408\u5e76\u5b9e\u73b0\u7684\u3002\u76f8\u53cd&#xff0c;\u7c7b\u578b\u522b\u540d\u662f\u5c01\u95ed\u7684&#xff0c;\u4e0d\u80fd\u5728\u521d\u6b21\u5b9a\u4e49\u540e\u518d\u589e\u52a0\u65b0\u6210\u5458\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578b\u522b\u540d\u66f4\u5177\u786e\u5b9a\u6027&#xff0c;\u4e00\u65e6\u5b9a\u4e49\u4e0d\u4f1a\u88ab\u5077\u5077\u4fee\u6539\u3002\u5f00\u653e\u6027\u6709\u5229\u4e8e\u6269\u5c55\u5927\u578b\u5e94\u7528\u6216\u5e93\u7684\u7c7b\u578b&#xff0c;\u4f46\u4e5f\u53ef\u80fd\u5728\u65e0\u610f\u4e2d\u5bfc\u81f4\u51b2\u7a81\u6216\u96be\u4ee5\u8ddf\u8e2a\u6765\u6e90\u3002\u56e0\u6b64&#xff0c;\u5728\u9700\u8981\u63d2\u4ef6\u5f0f\u6269\u5c55\u65f6\u9009\u63a5\u53e3&#xff0c;\u9700\u8981\u56fa\u5b9a\u5951\u7ea6\u65f6\u9009\u7c7b\u578b\u522b\u540d&#xff0c;\u8fd9\u53d6\u51b3\u4e8e\u517c\u5bb9\u6027\u7684\u9700\u6c42\u3002<\/p>\n<p>\u63a5\u53e3\u4e0e\u7c7b\u578b\u53ef\u4e92\u76f8\u517c\u5bb9&#xff1a; \u867d\u7136\u8bed\u6cd5\u4e0a\u6709\u6240\u533a\u522b&#xff0c;\u4f46\u63a5\u53e3\u5b9a\u4e49\u7684\u7c7b\u578b\u548c\u7c7b\u578b\u522b\u540d\u5b9a\u4e49\u7684\u7c7b\u578b\u5728 TypeScript \u7684\u7ed3\u6784\u7c7b\u578b\u7cfb\u7edf\u4e0b\u5b8c\u5168\u517c\u5bb9\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u4e00\u4e2a\u7528\u63a5\u53e3\u58f0\u660e\u7684\u5bf9\u8c61\u7c7b\u578b\u53ef\u4ee5\u8d4b\u503c\u7ed9\u4e00\u4e2a\u7b49\u4ef7\u7ed3\u6784\u7684\u7c7b\u578b\u522b\u540d&#xff0c;\u53cd\u4e4b\u4ea6\u7136\u3002\u751a\u81f3\u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u53ef\u4ee5\u4e92\u76f8\u6269\u5c55&#xff1a;\u63a5\u53e3\u80fd extends \u7c7b\u578b\u522b\u540d&#xff08;\u524d\u63d0\u7c7b\u578b\u522b\u540d\u8868\u793a\u7684\u662f\u5bf9\u8c61\u7ed3\u6784&#xff09;&#xff1b;\u7c7b\u578b\u522b\u540d\u4e5f\u80fd\u901a\u8fc7\u4ea4\u53c9\u7c7b\u578b\u5305\u542b\u63a5\u53e3\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p><span class=\"token keyword\">type<\/span> <span class=\"token class-name\"><span class=\"token constant\">X<\/span><\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> a<span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">interface<\/span> <span class=\"token class-name\"><span class=\"token constant\">Y<\/span><\/span> <span class=\"token punctuation\">{<\/span> b<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">interface<\/span> <span class=\"token class-name\"><span class=\"token constant\">Z<\/span><\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\"><span class=\"token constant\">X<\/span><\/span> <span class=\"token punctuation\">{<\/span> c<span class=\"token operator\">:<\/span> <span class=\"token builtin\">boolean<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span>    <span class=\"token comment\">\/\/ \u63a5\u53e3\u7ee7\u627f\u7c7b\u578b\u522b\u540d X<\/span><br \/>\n<span class=\"token keyword\">type<\/span> <span class=\"token class-name\"><span class=\"token constant\">W<\/span><\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token constant\">Y<\/span> <span class=\"token operator\">&amp;<\/span> <span class=\"token punctuation\">{<\/span> d<span class=\"token operator\">:<\/span> Date<span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u7c7b\u578b\u522b\u540d\u4ea4\u53c9\u63a5\u53e3 Y<\/span><\/p>\n<p><span class=\"token keyword\">let<\/span> z<span class=\"token operator\">:<\/span> <span class=\"token constant\">Z<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> a<span class=\"token operator\">:<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> c<span class=\"token operator\">:<\/span> <span class=\"token boolean\">true<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">let<\/span> w<span class=\"token operator\">:<\/span> <span class=\"token constant\">W<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> b<span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;hi&#034;<\/span><span class=\"token punctuation\">,<\/span> d<span class=\"token operator\">:<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Date<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4e0a\u4f8b\u4e2d\u63a5\u53e3 Z \u6210\u529f\u7ee7\u627f\u4e86\u7c7b\u578b\u522b\u540d X \u7684\u7ed3\u6784&#xff0c;\u800c\u7c7b\u578b\u522b\u540d W \u4e5f\u901a\u8fc7 &amp; \u5305\u542b\u4e86\u63a5\u53e3 Y \u7684\u5c5e\u6027&#xff0c;\u53ef\u89c1\u4e8c\u8005\u53ef\u4ee5\u7075\u6d3b\u6df7\u5408\u3002**\u7c7b\u5b9e\u73b0 (implements)**\u65b9\u9762&#xff0c;\u7c7b\u65e2\u53ef\u4ee5\u5b9e\u73b0\u63a5\u53e3&#xff0c;\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u578b\u522b\u540d&#xff08;\u53ea\u8981\u8be5\u7c7b\u578b\u522b\u540d\u5b9e\u9645\u662f\u5bf9\u8c61\u7ed3\u6784\u800c\u975e\u8054\u5408\u7c7b\u578b&#xff09;\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p><span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Shape<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">area<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span> <span class=\"token builtin\">number<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">type<\/span> <span class=\"token class-name\">Serializable<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">toJSON<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Circle<\/span> <span class=\"token keyword\">implements<\/span> <span class=\"token class-name\">Shape<\/span><span class=\"token punctuation\">,<\/span> Serializable <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u540c\u65f6\u5b9e\u73b0\u63a5\u53e3 Shape \u548c\u7c7b\u578b\u522b\u540d Serializable \u5b9a\u4e49\u7684\u8981\u6c42<\/span><br \/>\n  <span class=\"token function\">area<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* &#8230; *\/<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token function\">toJSON<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/* &#8230; *\/<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u9700\u8981\u6ce8\u610f&#xff0c;\u5982\u679c\u7c7b\u578b\u522b\u540d\u662f\u8054\u5408\u7c7b\u578b\u6216\u5176\u4ed6\u975e\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u7c7b\u65e0\u6cd5\u7528 implements \u5b9e\u73b0\u5b83&#xff08;\u56e0\u4e3a\u7c7b\u53ea\u80fd\u6709\u4e00\u79cd\u5177\u4f53\u7ed3\u6784&#xff0c;\u5b9e\u73b0\u8054\u5408\u7c7b\u578b\u6ca1\u6709\u610f\u4e49&#xff09;\u3002<\/p>\n<p>\u4ea4\u53c9\u7c7b\u578b vs \u63a5\u53e3\u7ee7\u627f&#xff1a; \u5f53\u9700\u8981\u7ec4\u5408\u591a\u4e2a\u7c7b\u578b\u65f6&#xff0c;\u63a5\u53e3\u53ef\u4ee5\u4f7f\u7528\u7ee7\u627f\u6765\u6241\u5e73\u5316\u5730\u5408\u5e76\u5c5e\u6027&#xff0c;\u800c\u7c7b\u578b\u522b\u540d\u901a\u8fc7 &amp; \u4ea4\u53c9\u6765\u53e0\u52a0\u7c7b\u578b\u3002\u4e8c\u8005\u751f\u6210\u7684\u7ed3\u679c\u5728\u7c7b\u578b\u68c0\u67e5\u4e0a\u5927\u4f53\u76f8\u540c&#xff0c;\u4f46\u6709\u4e00\u4e9b\u5fae\u5999\u533a\u522b&#xff1a;<\/p>\n<ul>\n<li>\u63a5\u53e3\u7ee7\u627f\u4f1a\u68c0\u67e5\u5c5e\u6027\u51b2\u7a81&#xff0c;\u5982\u679c\u7236\u63a5\u53e3\u4e2d\u6709\u540c\u540d\u5c5e\u6027\u800c\u5b50\u63a5\u53e3\u8bd5\u56fe\u4ee5\u4e0d\u540c\u7c7b\u578b\u8986\u76d6\u5b83&#xff0c;\u5c06\u76f4\u63a5\u62a5\u9519&#xff0c;\u8981\u6c42\u5f00\u53d1\u8005\u6f84\u6e05\u7c7b\u578b\u5173\u7cfb\u3002\u8fd9\u79cd\u884c\u4e3a\u6709\u52a9\u4e8e\u53ca\u65e9\u53d1\u73b0\u903b\u8f91\u9519\u8bef\u3002<\/li>\n<li>\u4ea4\u53c9\u7c7b\u578b\u5bf9\u5c5e\u6027\u51b2\u7a81\u66f4\u4e3a\u5bbd\u677e&#xff0c;\u5b83\u4e0d\u4f1a\u7acb\u5373\u62a5\u9519&#xff0c;\u800c\u662f\u5408\u5e76\u6210\u4e00\u4e2a\u4e0d\u53ef\u80fd\u6ee1\u8db3\u7684\u7c7b\u578b&#xff08;\u5982\u540c\u540d\u5c5e\u6027\u7c7b\u578b\u4e0d\u517c\u5bb9\u65f6\u4f1a\u63a8\u5bfc\u4e3a never \u7c7b\u578b&#xff09;\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u8be5\u5c5e\u6027\u5b9e\u9645\u4e0d\u53ef\u7528\u4f46\u672a\u663e\u5f0f\u62a5\u9519&#xff0c;\u57cb\u4e0b\u9690\u60a3\u3002<\/li>\n<li>\u6027\u80fd\u65b9\u9762&#xff0c;TypeScript \u5b98\u65b9\u8bf4\u660e\u5728\u7ec4\u5408\u591a\u4e2a\u7c7b\u578b\u65f6&#xff0c;\u63a5\u53e3\u7684\u7ee7\u627f\u901a\u5e38\u6bd4\u4ea4\u53c9\u7c7b\u578b\u66f4\u9ad8\u6548\u3002\u56e0\u4e3a\u63a5\u53e3\u6269\u5c55\u4ea7\u751f\u7684\u662f\u4e00\u4e2a\u6241\u5e73\u7684\u5355\u4e00\u5bf9\u8c61\u7c7b\u578b&#xff0c;\u7c7b\u578b\u5173\u7cfb\u8fd8\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u7f13\u5b58&#xff1b;\u800c\u4ea4\u53c9\u7c7b\u578b\u662f\u9012\u5f52\u5408\u5e76\u7684&#xff0c;\u5d4c\u5957\u4ea4\u53c9\u53ef\u80fd\u5728\u68c0\u67e5\u65f6\u53cd\u590d\u5c55\u5f00\u3002\u5c24\u5176\u5728\u5927\u578b\u9879\u76ee\u4e2d&#xff0c;\u8fc7\u5ea6\u4f7f\u7528\u590d\u6742\u7684\u4ea4\u53c9\u7c7b\u578b\u53ef\u80fd\u4f7f\u7c7b\u578b\u68c0\u67e5\u7f13\u6162\u3002\u57fa\u4e8e\u8fd9\u4e00\u539f\u56e0&#xff0c;\u6709\u6027\u80fd\u8003\u8651\u65f6\u5efa\u8bae\u4f18\u5148\u4f7f\u7528\u63a5\u53e3\u7ee7\u627f\u6765\u7ec4\u5408\u7c7b\u578b&#xff0c;\u800c\u975e\u4ea4\u53c9\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u9012\u5f52\u4e0e\u81ea\u5f15\u7528&#xff1a; \u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u90fd\u652f\u6301\u9012\u5f52\u5b9a\u4e49&#xff0c;\u5373\u7c7b\u578b\u4e2d\u5f15\u7528\u81ea\u8eab\u7528\u4e8e\u8868\u793a\u5d4c\u5957\u7ed3\u6784\u3002\u4f8b\u5982\u5b9a\u4e49\u94fe\u8868\u8282\u70b9&#xff1a;<\/p>\n<p><span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Node<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  value<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  next<span class=\"token operator\">?<\/span><span class=\"token operator\">:<\/span> Node<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u63a5\u53e3\u9012\u5f52\u5f15\u7528\u81ea\u8eab<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">type<\/span> <span class=\"token class-name\">NodeType<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  value<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  next<span class=\"token operator\">?<\/span><span class=\"token operator\">:<\/span> NodeType<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u7c7b\u578b\u522b\u540d\u9012\u5f52\u5f15\u7528\u81ea\u8eab<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4e24\u79cd\u65b9\u5f0f\u5747\u53ef\u63cf\u8ff0\u8fd9\u79cd\u9012\u5f52\u7ed3\u6784\u3002\u4e0d\u8fc7\u5728\u66f4\u590d\u6742\u7684\u9012\u5f52\u573a\u666f&#xff08;\u6bd4\u5982\u5229\u7528\u6761\u4ef6\u7c7b\u578b\u9012\u5f52\u8ba1\u7b97&#xff09;&#xff0c;\u53ea\u80fd\u7528\u7c7b\u578b\u522b\u540d\u5b9e\u73b0&#xff0c;\u56e0\u4e3a\u63a5\u53e3\u65e0\u6cd5\u7f16\u5199\u6761\u4ef6\u5224\u65ad\u3002\u6b64\u5916&#xff0c;TypeScript \u5bf9\u7c7b\u578b\u522b\u540d\u7684\u9012\u5f52\u590d\u6742\u5ea6\u6709\u4e00\u5b9a\u9650\u5236&#xff0c;\u5982\u679c\u5d4c\u5957\u8fc7\u6df1\u53ef\u80fd\u51fa\u73b0\u7f16\u8bd1\u6027\u80fd\u95ee\u9898&#xff0c;\u9700\u8981\u501f\u52a9\u5c3e\u9012\u5f52\u4f18\u5316\u7b49\u9ad8\u7ea7\u6280\u5de7\u3002\u4f46\u8fd9\u4e9b\u5c5e\u4e8e\u6781\u7aef\u60c5\u51b5&#xff0c;\u4e00\u822c\u7684\u9012\u5f52\u5f15\u7528\u63a5\u53e3\u548c\u7c7b\u578b\u8868\u73b0\u76f8\u540c\u3002<\/p>\n<p>\u517c\u5bb9\u7b2c\u4e09\u65b9\u5e93\u7c7b\u578b\u7cfb\u7edf&#xff1a; \u6269\u5c55\u6027\u4e5f\u4f53\u73b0\u4e3a\u80fd\u5426\u65e0\u7f1d\u4e0e\u7b2c\u4e09\u65b9\u5e93\u7684\u7c7b\u578b\u5b9a\u4e49\u4ea4\u4e92\u3002\u591a\u6570\u60c5\u51b5\u4e0b\u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u90fd\u80fd\u5f88\u597d\u5730\u517c\u5bb9\u4f7f\u7528\u3002\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>Redux \u7684 connect \u7b49\u9ad8\u9636\u7ec4\u4ef6\u9700\u8981\u5c06\u591a\u4e2a\u6765\u6e90\u7684 props \u7c7b\u578b\u5408\u5e76&#xff0c;\u8fd9\u65f6\u503e\u5411\u7528\u63a5\u53e3\u7ee7\u627f\u8ba9\u7c7b\u578b\u66f4\u6e05\u6670\u3002<\/li>\n<li>\u4e00\u4e9b\u5e93\u4f1a\u5bfc\u51fa\u8054\u5408\u7c7b\u578b\u4f5c\u4e3aAPI&#xff0c;\u5982 GraphQL \u7684\u8fd4\u56de\u53ef\u80fd\u662f\u591a\u79cd\u7c7b\u578b\u7684\u8054\u5408&#xff0c;\u5219\u4f1a\u4ee5\u7c7b\u578b\u522b\u540d\u63d0\u4f9b\u3002\u4f60\u7684\u4ee3\u7801\u53ef\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b type \u5b9a\u4e49&#xff0c;\u6216\u7528\u63a5\u53e3\u53bb\u7ee7\u627f\u5176\u4e2d\u7684\u5bf9\u8c61\u7c7b\u578b\u90e8\u5206&#xff0c;\u90fd\u6ca1\u6709\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p>\u603b\u4e4b&#xff0c;\u5728\u6269\u5c55\u6027\u65b9\u9762&#xff0c;\u63a5\u53e3\u63d0\u4f9b\u4e86\u66f4\u52a8\u6001\u5f00\u653e\u7684\u673a\u5236&#xff0c;\u9002\u5408\u9700\u8981\u88ab\u6269\u5145\u548c\u7ee7\u627f\u7684\u573a\u666f&#xff1b;\u7c7b\u578b\u522b\u540d\u66f4\u9759\u6001\u5c01\u95ed&#xff0c;\u9002\u5408\u660e\u786e\u6700\u7ec8\u5f62\u72b6\u3001\u4e0d\u5e0c\u671b\u88ab\u6539\u53d8\u7684\u573a\u666f\u3002\u4e8c\u8005\u7684\u7c7b\u578b\u517c\u5bb9\u6027\u5f88\u597d&#xff0c;\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6df7\u5408\u4f7f\u7528\u3002\u5b98\u65b9\u5efa\u8bae\u5bf9\u4e8e\u516c\u5171\u66b4\u9732\u7684API\u7c7b\u578b&#xff0c;\u4f18\u5148\u4f7f\u7528\u63a5\u53e3&#xff0c;\u56e0\u4e3a\u63a5\u53e3\u66f4\u6613\u6269\u5c55\u7ef4\u62a4\u3002<\/p>\n<h3>4. \u7f16\u8bd1\u540e\u884c\u4e3a<\/h3>\n<p>\u65e0\u8bba\u4f7f\u7528 interface \u8fd8\u662f type&#xff0c;\u5b83\u4eec\u90fd\u53ea\u5b58\u5728\u4e8e\u7f16\u8bd1\u65f6&#xff0c;\u5728\u7f16\u8bd1\u540e\u7684 JavaScript \u4e2d\u4e0d\u4f1a\u751f\u6210\u4efb\u4f55\u5b9e\u9645\u4ee3\u7801\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u8fd9\u4e24\u79cd\u7c7b\u578b\u5b9a\u4e49\u65b9\u5f0f\u5bf9\u8fd0\u884c\u65f6\u6027\u80fd\u6ca1\u6709\u76f4\u63a5\u5f71\u54cd&#xff0c;\u7eaf\u7cb9\u662f\u4e3a\u4e86\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u548cIDE\u667a\u80fd\u63d0\u793a\u670d\u52a1\u3002<\/p>\n<p>\u5177\u4f53\u800c\u8a00&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u63a5\u53e3\u5728\u7f16\u8bd1\u65f6\u88ab\u64e6\u9664\u3002\u6bd4\u5982&#xff1a;<\/p>\n<p> <span class=\"token keyword\">interface<\/span> <span class=\"token class-name\">Person<\/span> <span class=\"token punctuation\">{<\/span> name<span class=\"token operator\">:<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">const<\/span> p<span class=\"token operator\">:<\/span> Person <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> name<span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;Alice&#034;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7f16\u8bd1\u4e3aJS\u540e\u53ea\u5269\u4e0b { name: &#034;Alice&#034; } \u8fd9\u4e2a\u5bf9\u8c61\u8d4b\u503c&#xff0c;\u63a5\u53e3 Person \u4e0d\u4f1a\u51fa\u73b0\u5728\u8fd0\u884c\u65f6\u4ee3\u7801\u4e2d\u3002\u63a5\u53e3\u4e3b\u8981\u7528\u4e8e\u7f16\u8bd1\u9636\u6bb5\u7684\u7c7b\u578b\u9a8c\u8bc1&#xff0c;\u5728\u8fd0\u884c\u65f6\u6ca1\u6709\u4efb\u4f55\u68c0\u67e5\u529f\u80fd\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u578b\u522b\u540d\u540c\u6837\u4e0d\u4f1a\u4ea7\u751f\u8fd0\u884c\u65f6\u4ee3\u7801\u3002\u5b83\u53ea\u662f\u4e3a\u4e00\u6bb5\u7c7b\u578b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u5b57&#xff0c;\u5728\u7f16\u8bd1\u9636\u6bb5\u8d77\u4f5c\u7528\u3002\u4f8b\u5982&#xff1a;<\/p>\n<p> <span class=\"token keyword\">type<\/span> <span class=\"token class-name\"><span class=\"token constant\">ID<\/span><\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">number<\/span> <span class=\"token operator\">|<\/span> <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">let<\/span> userId<span class=\"token operator\">:<\/span> <span class=\"token constant\">ID<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">123<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7f16\u8bd1\u8f93\u51fa\u7684JS\u4ee3\u7801\u91cc\u4e5f\u53ea\u6709 let userId &#061; 123;&#xff0c;\u7c7b\u578b\u522b\u540d ID \u4e0d\u4f1a\u5b58\u5728\u3002\u7c7b\u578b\u522b\u540d\u4e5f\u65e0\u6cd5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8bbf\u95ee\u6216\u68c0\u67e5\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7531\u4e8e\u8fd9\u4e24\u8005\u90fd\u6ca1\u6709\u8fd0\u884c\u65f6\u5f00\u9500&#xff0c;\u5982\u679c\u60a8\u62c5\u5fc3\u67d0\u79cd\u7c7b\u578b\u5b9a\u4e49\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u989d\u5916\u7684JS\u4ee3\u7801&#xff0c;\u53ef\u4ee5\u653e\u5fc3\u2014\u2014\u5728\u8fd9\u70b9\u4e0a\u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u7684\u884c\u4e3a\u662f\u7b49\u4ef7\u7684\u3002\u5b83\u4eec\u548c TypeScript \u7684\u5176\u4ed6\u7c7b\u578b\u4e00\u6837&#xff0c;\u90fd\u662f\u201c\u7f16\u8bd1\u65f6\u827a\u672f\u54c1\u201d&#xff0c;\u4e0d\u4f1a\u50cf\u7c7b (class) \u90a3\u6837\u5728JS\u4e2d\u4fdd\u7559\u6784\u9020\u51fd\u6570\u6216\u65b9\u6cd5\u3002\u6b63\u56e0\u4e3a\u5982\u6b64&#xff0c;\u7528\u63a5\u53e3\u6216\u7c7b\u578b\u522b\u540d\u5b9a\u4e49\u7684\u7c7b\u578b\u4e0d\u80fd\u7528\u4e8e instanceof \u7b49\u8fd0\u884c\u65f6\u64cd\u4f5c&#xff0c;\u4e5f\u65e0\u6cd5\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u7c7b\u578b\u7ed3\u6784&#xff1b;\u4e00\u5207\u7c7b\u578b\u68c0\u67e5\u90fd\u5728\u7f16\u8bd1\u9636\u6bb5\u5b8c\u6210\u3002<\/p>\n<p>\u63d0\u793a&#xff1a; \u5982\u679c\u53ea\u5bfc\u5165\u4e86\u7c7b\u578b\u800c\u672a\u4f7f\u7528\u4efb\u4f55\u503c&#xff0c;TypeScript \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5220\u9664\u8fd9\u4e9b\u5bfc\u5165&#xff08;\u6216\u9700\u8981\u4f7f\u7528 import type \u8bed\u6cd5&#xff09;\u4ee5\u786e\u4fdd\u4e0d\u4ea7\u751f\u672a\u4f7f\u7528\u7684\u8fd0\u884c\u65f6\u4ee3\u7801\u3002\u8fd9\u8fdb\u4e00\u6b65\u8bc1\u660e\u4e86\u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u7eaf\u7cb9\u662f\u96f6\u8fd0\u884c\u65f6\u5f71\u54cd\u7684\u3002<\/p>\n<h3>5. \u4e0e\u7b2c\u4e09\u65b9\u7c7b\u578b\u7cfb\u7edf\u96c6\u6210\u5efa\u8bae<\/h3>\n<p>\u5728\u4e0e React\u3001Vue\u3001GraphQL \u7b49\u7b2c\u4e09\u65b9\u5e93\u6216\u6846\u67b6\u534f\u540c\u4f7f\u7528 TypeScript \u65f6&#xff0c;\u9009\u62e9 interface \u6216 type \u9700\u8981\u8003\u8651\u8fd9\u4e9b\u751f\u6001\u7684\u60ef\u4f8b\u548c\u5de5\u5177\u94fe\u652f\u6301&#xff1a;<\/p>\n<ul>\n<li>\n<p>React \u96c6\u6210&#xff1a; \u5bf9\u4e8e React \u7ec4\u4ef6\u7684 Props \u548c State \u7c7b\u578b&#xff0c;\u5b98\u65b9\u548c\u793e\u533a\u5747\u8868\u793a\u53ef\u4ee5\u4f7f\u7528\u63a5\u53e3\u6216\u7c7b\u578b\u522b\u540d\u5b9a\u4e49\u5bf9\u8c61\u7c7b\u578b\u3002\u4e0d\u8fc7\u5728\u5b9e\u8df5\u4e2d&#xff0c;\u4f7f\u7528\u63a5\u53e3\u5b9a\u4e49 Props \u66f4\u4e3a\u5e38\u89c1\u3002\u7406\u7531\u5305\u62ec\u63a5\u53e3\u53ef\u6269\u5c55\u3001\u9519\u8bef\u4fe1\u606f\u53cb\u597d&#xff0c;\u4ee5\u53ca\u5728\u9700\u8981\u4ece\u5176\u4ed6\u7c7b\u578b\u6269\u5c55\u65f6\u4e66\u5199\u66f4\u7b80\u6d01\u3002\u5f88\u591a\u6559\u7a0b\u548c\u4ee3\u7801\u793a\u4f8b\u90fd\u91c7\u7528\u63a5\u53e3&#xff0c;\u4f8b\u5982 React \u5b98\u65b9 TypeScript Cheatsheet \u5c31\u91c7\u7528 interface AppProps { &#8230; } \u7684\u5f62\u5f0f\u5b9a\u4e49 Props\u3002\u800c\u5f53Props\u6d89\u53ca\u4ea4\u53c9\u7c7b\u578b\u7ec4\u5408\u65f6&#xff08;\u4f8b\u5982\u7ec4\u4ef6\u65e2\u6709\u81ea\u8eab\u5c5e\u6027\u53c8\u8981\u5305\u542b HTML \u5143\u7d20\u5c5e\u6027&#xff09;&#xff0c;\u63a5\u53e3\u7684 extends \u6bd4\u7c7b\u578b\u522b\u540d\u7684 &amp; \u66f4\u5229\u4e8e\u7c7b\u578b\u68c0\u67e5\u6027\u80fd\u3002\u56e0\u6b64\u63a8\u8350&#xff1a;React \u9879\u76ee\u4e2d&#xff0c;\u7b80\u5355Props\u4efb\u9009\u5176\u4e00\u5373\u53ef&#xff0c;\u4f46\u590d\u6742Props\u6216\u516c\u5171Props\u7c7b\u578b\u503e\u5411\u4e8e\u4f7f\u7528\u63a5\u53e3\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f&#xff0c;\u4e00\u4e9b\u56e2\u961f\u53ef\u80fd\u901a\u8fc7 ESLint \u89c4\u5219&#xff08;\u5982&#064;typescript-eslint\/consistent-type-definitions&#xff09;\u7edf\u4e00\u7ea6\u5b9a Props \u7528\u63a5\u53e3\u6216\u7c7b\u578b&#xff0c;\u8fd9\u4e3b\u8981\u662f\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\u6027\u8003\u8651\u3002<\/p>\n<\/li>\n<li>\n<p>Vue \u96c6\u6210&#xff1a; \u5728 Vue 3 \u7684\u7ec4\u5408\u5f0f API (Composition API) \u4e2d&#xff0c;\u4f7f\u7528 defineProps&lt;Type&gt;() \u6765\u5b9a\u4e49\u7ec4\u4ef6 props \u7c7b\u578b\u3002Vue \u7684\u8bbe\u8ba1\u5bf9\u63a5\u53e3\u548c\u7c7b\u578b\u522b\u540d\u5747\u652f\u6301&#xff0c;\u4f46\u5728 Vue 3.2 \u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d&#xff0c;\u6709\u4e00\u9879\u9650\u5236&#xff1a;defineProps \u7684\u6cdb\u578b\u53c2\u6570\u53ea\u80fd\u662f\u5b57\u9762\u91cf\u5bf9\u8c61\u7c7b\u578b\u6216\u672c\u5730\u63a5\u53e3&#xff0c;\u4e0d\u652f\u6301\u76f4\u63a5\u5f15\u7528\u5bfc\u5165\u7684\u7c7b\u578b\u522b\u540d\u3002\u4e3a\u7ed5\u8fc7\u8fd9\u4e00\u9650\u5236&#xff0c;\u8bb8\u591a Vue 3 \u7528\u6237\u5f53\u65f6\u91c7\u7528\u63a5\u53e3\u6765\u5b9a\u4e49 Props&#xff0c;\u7136\u540e\u5728\u7ec4\u4ef6\u5185\u4f7f\u7528\u8be5\u63a5\u53e3&#xff08;\u6216\u8005\u76f4\u63a5\u5728 &lt;script setup&gt; \u5185\u58f0\u660e\u63a5\u53e3\u518d\u7528&#xff09;\u3002\u4ece Vue 3.3 \u5f00\u59cb&#xff0c;\u8fd9\u4e00\u9650\u5236\u6709\u6240\u653e\u5bbd&#xff0c;defineProps \u5df2\u652f\u6301\u5f15\u7528\u90e8\u5206\u5bfc\u5165\u7684\u7c7b\u578b\u522b\u540d&#xff0c;\u4f46\u4ecd\u5bf9\u590d\u6742\u7c7b\u578b&#xff08;\u5982\u9012\u5f52\u7684\u6761\u4ef6\u7c7b\u578b&#xff09;\u6709\u9650\u5236\u3002\u7efc\u5408\u8003\u8651&#xff0c;\u5728 Vue \u9879\u76ee\u4e2d\u53ef\u4ee5&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u4f7f\u7528 Vue 3.2 \u6216\u66f4\u4f4e\u7248\u672c&#xff0c;\u5efa\u8bae\u4f7f\u7528\u63a5\u53e3\u5b9a\u4e49\u7ec4\u4ef6 Props&#xff08;\u6216\u8005\u76f4\u63a5\u5728 defineProps&lt;&gt; \u5185\u4e66\u5199\u5bf9\u8c61\u7c7b\u578b\u5b57\u9762\u91cf&#xff09;&#xff0c;\u4ee5\u786e\u4fdd\u7c7b\u578b\u80fd\u6b63\u786e\u88ab Vue \u7f16\u8bd1\u5668\u89e3\u6790\u3002<\/li>\n<li>\u5728 Vue 3.3&#043; \u4e2d&#xff0c;\u63a5\u53e3\u6216\u7c7b\u578b\u522b\u540d\u7686\u53ef&#xff0c;\u6839\u636e\u56e2\u961f\u504f\u597d\u9009\u62e9\u3002\u5982\u679c Props \u7c7b\u578b\u76f8\u5bf9\u7b80\u5355&#xff0c;\u76f4\u63a5\u5728 defineProps&lt;&gt; \u4e2d\u5199\u5bf9\u8c61\u5b57\u9762\u91cf\u6700\u76f4\u89c2&#xff1b;\u5982\u9700\u5728\u591a\u4e2a\u7ec4\u4ef6\u95f4\u590d\u7528 Props \u7c7b\u578b&#xff0c;\u4ecd\u53ef\u5bfc\u51fa\u63a5\u53e3\u6216\u7c7b\u578b\u522b\u540d\u4f9b defineProps \u5f15\u7528\u3002\u63a5\u53e3\u7684\u597d\u5904\u662f\u53ef\u5c40\u90e8\u58f0\u660e\u3001\u4e0d\u7528\u663e\u5f0f\u5bfc\u5165\u6587\u4ef6&#xff08;\u5728 &lt;script setup&gt; \u4e2d\u76f4\u63a5\u58f0\u660e\u63a5\u53e3\u5373\u53ef\u4f7f\u7528&#xff09;&#xff0c;\u7c7b\u578b\u522b\u540d\u5219\u9700\u8981 import type \u5bfc\u5165\u5916\u90e8\u5b9a\u4e49\u3002<\/li>\n<li>\u6ce8\u610f\u907f\u514d\u5728 defineProps \u7684\u7c7b\u578b\u4e2d\u4f7f\u7528\u8fc7\u4e8e\u590d\u6742\u7684\u7c7b\u578b\u8fd0\u7b97&#xff08;\u6bd4\u5982\u5168\u5c40\u8054\u5408\u6216\u590d\u6742\u6761\u4ef6&#xff09;&#xff0c;\u56e0\u4e3a Vue \u7684\u7c7b\u578b\u63a8\u5bfc\u5bf9\u8fd9\u4e9b\u60c5\u51b5\u652f\u6301\u6709\u9650\u3002\u5982\u6709\u9700\u8981&#xff0c;\u53ef\u8003\u8651\u5c06\u590d\u6742\u7c7b\u578b\u62c6\u5206&#xff0c;\u6216\u8005\u5728 Prop \u7c7b\u578b\u4e2d\u4f7f\u7528\u63a5\u53e3\u63cf\u8ff0\u57fa\u672c\u7ed3\u6784&#xff0c;\u518d\u901a\u8fc7\u989d\u5916\u6821\u9a8c\u6765\u786e\u4fdd\u66f4\u590d\u6742\u7684\u7ea6\u675f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>GraphQL \u96c6\u6210&#xff1a; \u5728\u4f7f\u7528 GraphQL \u6216 Apollo \u65f6&#xff0c;\u4f1a\u7ecf\u5e38\u901a\u8fc7\u4ee3\u7801\u751f\u6210\u5668\u5c06 GraphQL \u6a21\u5f0f(schema)\u751f\u6210\u5bf9\u5e94\u7684 TypeScript \u7c7b\u578b\u3002\u8001\u7248\u7684 Apollo Codegen \u9ed8\u8ba4\u751f\u6210 \u63a5\u53e3 \u6765\u8868\u793a GraphQL \u7c7b\u578b&#xff0c;\u4f46\u73b0\u4ee3\u7684 GraphQL Code Generator&#xff08;\u7531 The Guild \u5f00\u53d1\u7684\u5de5\u5177&#xff09;\u66f4\u503e\u5411\u4e8e\u751f\u6210 \u7c7b\u578b\u522b\u540d\u3002\u539f\u56e0\u5728\u4e8e&#xff1a;GraphQL \u6a21\u5f0f\u4e2d\u6709\u8bb8\u591a\u8054\u5408\u7c7b\u578b\u548c\u53ef\u9009\u5b57\u6bb5&#xff0c;\u76f4\u63a5\u6620\u5c04\u4e3a TypeScript \u63a5\u53e3\u4f1a\u53d7\u9650&#xff0c;\u800c\u7c7b\u578b\u522b\u540d\u66f4\u7075\u6d3b&#xff0c;\u53ef\u8868\u793a\u8054\u5408\u3001\u53ef\u9009\u4ee5\u53ca\u7d22\u5f15\u7b7e\u540d\u7b49\u3002\u4f8b\u5982 GraphQL \u4e2d\u7684 union SearchResult &#061; Photo | User&#xff0c;\u751f\u6210 TS \u65f6\u5fc5\u987b\u662f type SearchResult &#061; Photo \\\\| User \u624d\u80fd\u5de5\u4f5c&#xff0c;\u63a5\u53e3\u65e0\u6cd5\u76f4\u63a5\u5bf9\u5e94\u3002\u6b64\u5916&#xff0c;\u793e\u533a\u5728\u5b9e\u8df5\u4e2d\u53d1\u73b0&#xff0c;\u7528\u7c7b\u578b\u522b\u540d\u8868\u793a\u7eaf\u6570\u636e\u7ed3\u6784\u66f4\u8d34\u5207&#xff0c;\u800c\u63a5\u53e3\u8bed\u4e49\u4e0a\u8868\u793a\u53ef\u5b9e\u4f8b\u5316\u7684\u5951\u7ea6\u5e76\u4e0d\u5b8c\u5168\u543b\u5408 GraphQL \u7c7b\u578b\u7684\u7528\u9014\u3002\u56e0\u6b64\u5efa\u8bae&#xff1a;\u4f7f\u7528 GraphQL Codegen \u751f\u6210\u7c7b\u578b\u65f6&#xff0c;\u9664\u975e\u6709\u7279\u6b8a\u9700\u6c42&#xff0c;\u4fdd\u6301\u9ed8\u8ba4\u7684\u7c7b\u578b\u522b\u540d\u8f93\u51fa\u5373\u53ef\u3002\u5982\u679c\u9879\u76ee\u4ee5\u524d\u4f7f\u7528 Apollo Codegen \u4e14\u751f\u6210\u7684\u662f\u63a5\u53e3&#xff0c;\u53ef\u4ee5\u5e73\u6ed1\u8fc1\u79fb\u5230\u7c7b\u578b\u522b\u540d\u5f62\u5f0f&#xff0c;\u7c7b\u578b\u68c0\u67e5\u4e0a\u5e94\u65e0\u5dee\u5f02\u3002\u5728\u5e94\u7528\u4ee3\u7801\u4e2d&#xff0c;\u65e0\u9700\u5173\u5fc3\u8fd9\u4e9b\u7c7b\u578b\u662f\u63a5\u53e3\u8fd8\u662f\u7c7b\u578b\u522b\u540d\u2014\u2014\u5b83\u4eec\u672c\u8d28\u90fd\u662f\u6570\u636e\u7684\u7c7b\u578b\u63cf\u8ff0&#xff0c;\u53ea\u8981\u7ed3\u6784\u4e00\u81f4\u5c31\u53ef\u4ee5\u76f8\u4e92\u8d4b\u503c\u4f7f\u7528\u3002\u552f\u4e00\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u5982\u679c\u6253\u7b97\u57fa\u4e8e\u751f\u6210\u7684\u7c7b\u578b\u505a\u8fdb\u4e00\u6b65\u62d3\u5c55\u6216\u9762\u5411\u5bf9\u8c61\u5efa\u6a21&#xff0c;\u6bd4\u5982\u5b9a\u4e49\u7c7b\u53bb\u5b9e\u73b0\u67d0\u4e2a GraphQL \u8fd4\u56de\u63a5\u53e3&#xff0c;\u90a3\u4e48\u53ef\u80fd\u8981\u8c03\u6574\u7b56\u7565&#xff08;\u901a\u5e38\u5f88\u5c11\u8fd9\u6837\u505a&#xff0c;\u56e0\u4e3a\u89e3\u6790\u540e\u7684\u6570\u636e\u5f80\u5f80\u4ee5\u76f4\u63a5\u5bf9\u8c61\u4f7f\u7528&#xff09;\u3002\u603b\u4f53\u800c\u8a00&#xff0c;GraphQL \u7684\u7c7b\u578b\u66f4\u9002\u5408\u4f5c\u4e3a\u7c7b\u578b\u522b\u540d&#xff0c;\u8fd9\u4e5f\u662f\u5f53\u524d\u5927\u591a\u6570 GraphQL TypeScript \u96c6\u6210\u65b9\u6848\u7684\u9009\u62e9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u4ed6\u5e93 (\u5982Redux\/React-Redux, Express\u7b49)&#xff1a; \u901a\u5e38\u5e93\u4f1a\u5728\u6587\u6863\u4e2d\u8bf4\u660e\u7c7b\u578b\u5982\u4f55\u5b9a\u4e49\u548c\u6269\u5c55\u3002\u9075\u5faa\u5b83\u4eec\u7684\u5efa\u8bae\u9009\u62e9 interface \u6216 type \u5373\u53ef\u3002\u4f8b\u5982 React-Redux \u7684 connect \u7c7b\u578b\u5f88\u590d\u6742&#xff0c;\u4e00\u822c\u4e0d\u624b\u5199\u800c\u4ea4\u7531\u5e93\u5b9a\u4e49&#xff0c;\u4f46\u5982\u679c\u9700\u8981\u81ea\u5df1\u63cf\u8ff0 Props&#xff0c;\u4e5f\u63a8\u8350\u63a5\u53e3\u4f18\u5148\u3002\u8fd9\u4e9b\u5e93\u672c\u8eab\u7684\u7c7b\u578b\u58f0\u660e\u6587\u4ef6&#xff08;*.d.ts&#xff09;\u91cc&#xff0c;\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u5b83\u4eec\u5bfc\u51fa\u7684\u7c7b\u578b\u662f interface \u8fd8\u662f type&#xff0c;\u4ece\u800c\u5728\u6269\u5c55\u65f6\u9009\u62e9\u5339\u914d\u7684\u65b9\u5f0f\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u603b\u7ed3\u6700\u4f73\u5b9e\u8df5&#xff1a; \u7efc\u5408\u4ee5\u4e0a\u6bd4\u8f83\u548c\u573a\u666f\u5206\u6790&#xff0c;\u53ef\u4ee5\u6574\u7406\u51fa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\u4f9b\u53c2\u8003&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5bf9\u8c61\u6a21\u578b\u548cAPI\u5951\u7ea6&#xff1a;\u4f18\u5148\u4f7f\u7528 interface\u3002\u63a5\u53e3\u66f4\u80fd\u4f53\u73b0\u9762\u5411\u63a5\u53e3\u7f16\u7a0b\u7684\u601d\u60f3&#xff0c;\u652f\u6301\u58f0\u660e\u5408\u5e76\u5229\u4e8e\u6269\u5c55\u548c\u7ef4\u62a4&#xff0c;\u5bf9\u516c\u5171API&#xff08;\u5982\u5e93\u7684\u5bfc\u51fa\u7c7b\u578b\u3001\u7ec4\u4ef6Props&#xff09;\u5c24\u4e3a\u5408\u9002\u3002\u63a5\u53e3\u547d\u540d\u6e05\u6670&#xff0c;\u5728\u9519\u8bef\u4fe1\u606f\u4e2d\u66f4\u76f4\u89c2\u3002\u82e5\u65e5\u540e\u9700\u8981\u6269\u5c55\u5c5e\u6027&#xff0c;\u63a5\u53e3\u4e5f\u80fd\u65b9\u4fbf\u5730\u7ee7\u627f\u6216\u5408\u5e76\u3002<\/p>\n<\/li>\n<li>\n<p>\u8054\u5408\u7c7b\u578b\u6216\u7279\u6b8a\u7c7b\u578b&#xff1a;\u5fc5\u987b\u4f7f\u7528 type\u3002\u51e1\u662f\u63a5\u53e3\u4e0d\u652f\u6301\u7684\u573a\u666f&#xff08;\u57fa\u672c\u7c7b\u578b\u522b\u540d\u3001\u8054\u5408\u3001\u5143\u7ec4\u3001\u6761\u4ef6\u3001\u6620\u5c04\u7c7b\u578b\u7b49&#xff09;&#xff0c;type \u662f\u552f\u4e00\u9009\u62e9\u3002\u6bd4\u5982\u5b9a\u4e49\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u8054\u5408\u3001\u51fd\u6570\u91cd\u8f7d\u8054\u5408\u3001\u952e\u6620\u5c04\u5230\u65b0\u5bf9\u8c61\u7b49&#xff0c;\u6b64\u65f6\u7c7b\u578b\u522b\u540d\u65e0\u53ef\u66ff\u4ee3\u3002<\/p>\n<\/li>\n<li>\n<p>\u6027\u80fd\u8003\u91cf&#xff1a;\u5728\u9700\u8981\u7ec4\u5408\u591a\u4e2a\u7c7b\u578b\u65f6&#xff0c;\u5c3d\u91cf\u4f7f\u7528\u63a5\u53e3\u7684\u7ee7\u627f\u800c\u975e\u4ea4\u53c9\u7c7b\u578b&#xff0c;\u4ee5\u51cf\u8f7b\u7f16\u8bd1\u5668\u8d1f\u62c5\u3002\u5982\u679c\u5df2\u7ecf\u7528\u4e86\u5927\u91cf\u7c7b\u578b\u522b\u540d\u4ea4\u53c9&#xff0c;\u4e5f\u4e0d\u5fc5\u76f2\u76ee\u91cd\u6784&#xff0c;\u4f46\u53ef\u4ee5\u5728\u65b0\u4ee3\u7801\u4e2d\u907f\u514d\u8fc7\u6df1\u7684\u4ea4\u53c9\u53e0\u52a0\u3002\u5fae\u8f6f\u7684\u5b98\u65b9\u6027\u80fd\u6307\u5357\u5efa\u8bae\u201c\u4f7f\u7528\u63a5\u53e3\u6269\u5c55\u6765\u4ee3\u66ff\u4ea4\u53c9\u7c7b\u578b\u201d&#xff0c;\u4ee5\u83b7\u5f97\u66f4\u5feb\u7684\u7c7b\u578b\u8ba1\u7b97\u548c\u66f4\u53cb\u597d\u7684\u9519\u8bef\u663e\u793a\u3002<\/p>\n<\/li>\n<li>\n<p>\u56e2\u961f\u4e00\u81f4\u6027&#xff1a;\u786e\u4fdd\u56e2\u961f\u5728\u98ce\u683c\u4e0a\u8fbe\u6210\u4e00\u81f4&#xff0c;\u907f\u514d\u6709\u4eba\u7528\u63a5\u53e3\u6709\u4eba\u7528\u7c7b\u578b\u522b\u540d\u5b9a\u4e49\u76f8\u540c\u76ee\u7684\u7684\u7c7b\u578b\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f&#xff0c;\u5e94\u6709\u6e05\u6670\u7684\u7ea6\u5b9a\u3002\u4f8b\u5982\u4e00\u4e9b\u56e2\u961f\u89c4\u5b9a\u201c\u5bf9\u8c61\u7c7b\u578b\u4e00\u5f8b\u7528\u63a5\u53e3&#xff0c;\u9664\u975e\u9700\u8981\u7528 type\u201d&#xff0c;\u800c\u53e6\u4e00\u4e9b\u56e2\u961f\u53ef\u80fd\u503e\u5411\u201c\u80fd\u7528 type \u5c31\u7528 type&#xff0c;\u63a5\u53e3\u4ec5\u7528\u4e8e\u58f0\u660e\u5408\u5e76\u573a\u666f\u201d\u3002\u4e24\u79cd\u8def\u7ebf\u4e0a\u90fd\u6709\u4eba\u575a\u6301&#xff0c;\u5404\u6709\u7406\u7531\u3002\u5982\u679c\u8981\u53c2\u8003\u5b98\u65b9\u7acb\u573a&#xff0c;TypeScript \u624b\u518c\u63d0\u4f9b\u7684\u542f\u53d1\u5f0f\u662f\u5148\u7528\u63a5\u53e3&#xff0c;\u5fc5\u8981\u65f6\u518d\u7528\u7c7b\u578b\u522b\u540d\u3002\u8fd9\u4e00\u539f\u5219\u5728\u5b9e\u8df5\u4e2d\u4e5f\u88ab\u591a\u6570\u5f00\u53d1\u8005\u91c7\u7eb3\u3002\u5f53\u7136&#xff0c;\u6700\u7ec8\u9009\u62e9\u5e94\u7ed3\u5408\u9879\u76ee\u7279\u70b9\u548c\u5f00\u53d1\u8005\u7ecf\u9a8c\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u603b\u800c\u8a00\u4e4b&#xff0c;interface \u548c type \u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u53ef\u4ee5\u4e92\u6362\u4f7f\u7528&#xff0c;\u5173\u952e\u662f\u4e86\u89e3\u5b83\u4eec\u5404\u81ea\u7684\u8fb9\u754c\u4e0e\u4f18\u52bf\u3002\u5728\u5b9a\u4e49\u7b80\u5355\u7c7b\u578b\u65f6\u4e0d\u5fc5\u7ea0\u7ed3&#xff0c;\u4e24\u8005\u5747\u53ef\u6ee1\u8db3\u9700\u6c42\u3002\u4f46\u5728\u8bbe\u8ba1\u590d\u6742\u7cfb\u7edf\u7684\u7c7b\u578b\u65f6&#xff0c;\u5408\u7406\u9009\u62e9\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd&#xff1a;\u8ba9\u63a5\u53e3\u627f\u62c5\u6a21\u5757\u4e4b\u95f4\u7684\u5951\u7ea6\u89d2\u8272&#xff0c;\u8ba9\u7c7b\u578b\u522b\u540d\u53d1\u6325\u7c7b\u578b\u8868\u8fbe\u529b\u7684\u7279\u957f\u3002\u638c\u63e1\u4f55\u65f6\u7528\u597d\u5b83\u4eec&#xff0c;\u5c06\u4f7f TypeScript \u7684\u7c7b\u578b\u7cfb\u7edf\u4e3a\u4f60\u7684\u9879\u76ee\u4fdd\u9a7e\u62a4\u822a\u3002<\/p>\n<p>\u53c2\u8003\u6765\u6e90&#xff1a;<\/p>\n<ul>\n<li>TypeScript \u5b98\u65b9\u624b\u518c\u3001\u6027\u80fd\u6307\u5357\u548c Playground \u793a\u4f8b<\/li>\n<li>Stack Overflow \u7b49\u793e\u533a\u7ecf\u9a8c<\/li>\n<li>React\u3001Vue \u5b98\u65b9\u6587\u6863<\/li>\n<li>\u793e\u533a\u535a\u5ba2\u4e0e\u5b9e\u8df5\u6848\u4f8b<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb619\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf14\u6b21\u3002TypeScript \u4e2d\u7684interface\u4e0etype\u505a\u4e00\u4e2a\u6df1\u5165\u5bf9\u6bd4\uff0c\u5305\u62ec\u8bed\u6cd5\u7279\u6027\u3001\u4f7f\u7528\u573a\u666f\u3001\u517c\u5bb9\u6027\u4e0e\u6269\u5c55\u6027\uff0c\u4ee5\u53ca\u5e95\u5c42\u884c\u4e3a\u5dee\u5f02\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[187,568,87,3190],"topic":[],"class_list":["post-36924","post","type-post","status-publish","format-standard","hentry","category-server","tag-javascript","tag-typescript","tag-87","tag-3190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>TypeScript interface vs type \u5bf9\u6bd4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/36924.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TypeScript interface vs type \u5bf9\u6bd4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb619\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf14\u6b21\u3002TypeScript \u4e2d\u7684interface\u4e0etype\u505a\u4e00\u4e2a\u6df1\u5165\u5bf9\u6bd4\uff0c\u5305\u62ec\u8bed\u6cd5\u7279\u6027\u3001\u4f7f\u7528\u573a\u666f\u3001\u517c\u5bb9\u6027\u4e0e\u6269\u5c55\u6027\uff0c\u4ee5\u53ca\u5e95\u5c42\u884c\u4e3a\u5dee\u5f02\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/36924.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-12T17:03:31+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/36924.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/36924.html\",\"name\":\"TypeScript interface vs type \u5bf9\u6bd4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-12T17:03:31+00:00\",\"dateModified\":\"2025-05-12T17:03:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/36924.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/36924.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/36924.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TypeScript interface vs type \u5bf9\u6bd4\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TypeScript interface vs type \u5bf9\u6bd4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/36924.html","og_locale":"zh_CN","og_type":"article","og_title":"TypeScript interface vs type \u5bf9\u6bd4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb619\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf14\u6b21\u3002TypeScript \u4e2d\u7684interface\u4e0etype\u505a\u4e00\u4e2a\u6df1\u5165\u5bf9\u6bd4\uff0c\u5305\u62ec\u8bed\u6cd5\u7279\u6027\u3001\u4f7f\u7528\u573a\u666f\u3001\u517c\u5bb9\u6027\u4e0e\u6269\u5c55\u6027\uff0c\u4ee5\u53ca\u5e95\u5c42\u884c\u4e3a\u5dee\u5f02\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/36924.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-12T17:03:31+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/36924.html","url":"https:\/\/www.wsisp.com\/helps\/36924.html","name":"TypeScript interface vs type \u5bf9\u6bd4 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-12T17:03:31+00:00","dateModified":"2025-05-12T17:03:31+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/36924.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/36924.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/36924.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"TypeScript interface vs type \u5bf9\u6bd4"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/36924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=36924"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/36924\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=36924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=36924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=36924"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=36924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}