{"id":3099,"date":"2024-11-10T10:47:23","date_gmt":"2024-11-10T02:47:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3099\/"},"modified":"2024-11-10T10:47:23","modified_gmt":"2024-11-10T02:47:23","slug":"typescript%e5%bc%95l%e7%94%a8%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3099\/","title":{"rendered":"typescript\u5f15l\u7528\u7c7b\u578b"},"content":{"rendered":"<blockquote><p>\n  typescript \u4e2d\u7684\u5f15\u7528\u7c7b\u578b\u5141\u8bb8\u53d8\u91cf\u5f15\u7528\u53e6\u4e00\u5757\u5185\u5b58\u4e2d\u7684\u503c\uff0c\u53ef\u4f7f\u7528 any \u6216 never \u7c7b\u578b\u521b\u5efa\u3002\u5f15\u7528\u7c7b\u578b\u53ea\u80fd\u8d4b\u503c\u7ed9\u5f15\u7528\u7c7b\u578b\uff0c\u503c\u901a\u8fc7 * \u64cd\u4f5c\u7b26\u8bbf\u95ee\u3002\u5f15\u7528\u7c7b\u578b\u4e0e\u539f\u59cb\u7c7b\u578b\u4e0d\u540c\uff0c\u5b83\u5b58\u50a8\u7684\u662f\u503c\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u503c\uff0c\u5f71\u54cd\u503c\u4f20\u9012\u548c\u5185\u5b58\u7ba1\u7406\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/07\/2024110723332879621.jpg\" class=\"aligncenter\" title=\"typescript\u5f15l\u7528\u7c7b\u578b\u63d2\u56fe\" alt=\"typescript\u5f15l\u7528\u7c7b\u578b\u63d2\u56fe\" \/><\/p>\n<p><strong>TypeScript \u4e2d\u5f15\u7528\u7c7b\u578b<\/strong><\/p>\n<p>TypeScript \u4e2d\u7684\u5f15\u7528\u7c7b\u578b\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\uff0c\u5b83\u5c01\u88c5\u4e86\u5bf9\u53e6\u4e00\u4e2a\u7c7b\u578b\u7684\u5f15\u7528\u3002\u5f15\u7528\u7c7b\u578b\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u6216\u5c5e\u6027\u6765\u95f4\u63a5\u8bbf\u95ee\u53e6\u4e00\u5757\u5185\u5b58\u4e2d\u7684\u503c\u3002<\/p>\n<p><strong>\u521b\u5efa\u5f15\u7528\u7c7b\u578b<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u5f15\u7528\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528 any \u7c7b\u578b\u6216 never \u7c7b\u578b\u3002any \u7c7b\u578b\u8868\u793a\u53d8\u91cf\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff0c\u800c never \u7c7b\u578b\u8868\u793a\u53d8\u91cf\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ any \u7c7b\u578b\u5f15\u7528\nlet anyReference: any;\n\n\/\/ never \u7c7b\u578b\u5f15\u7528\nlet neverReference: never;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u5f15\u7528\u7c7b\u578b<\/strong><\/p>\n<p>\u5f15\u7528\u7c7b\u578b\u53ea\u80fd\u8d4b\u503c\u7ed9\u5176\u4ed6\u5f15\u7528\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>let reference1: any;\nlet reference2: any = reference1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f15\u7528\u7c7b\u578b\u7684\u503c\u901a\u8fc7\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 * \u8bbf\u95ee\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>reference2 = 10;\nconsole.log(*reference1); \/\/ \u8f93\u51fa\uff1a10<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5f15\u7528\u7c7b\u578b\u4e0e\u539f\u59cb\u7c7b\u578b<\/strong><\/p>\n<p>\u5f15\u7528\u7c7b\u578b\u4e0e\u539f\u59cb\u7c7b\u578b\u4e0d\u540c\uff0c\u56e0\u4e3a\u5f15\u7528\u7c7b\u578b\u5b58\u50a8\u7684\u662f\u5bf9\u53e6\u4e00\u5757\u5185\u5b58\u4e2d\u503c\u7684\u5f15\u7528\uff0c\u800c\u539f\u59cb\u7c7b\u578b\u5219\u5b58\u50a8\u5b9e\u9645\u503c\u3002\u8fd9\u610f\u5473\u7740\uff1a<\/p>\n<ul>\n<li> <strong>\u503c\u4f20\u9012\uff1a<\/strong>\u5f53\u5c06\u5f15\u7528\u7c7b\u578b\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u4f20\u9012\u7684\u662f\u5bf9\u8be5\u7c7b\u578b\u7684\u5f15\u7528\u7684\u526f\u672c\u3002\u8fd9\u610f\u5473\u7740\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u5f15\u7528\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4f46\u4e0d\u80fd\u4fee\u6539\u5f15\u7528\u7c7b\u578b\u5f15\u7528\u7684\u503c\u3002<\/li>\n<li> <strong>\u5185\u5b58\u7ba1\u7406\uff1a<\/strong>\u5f15\u7528\u7c7b\u578b\u7684\u503c\u5b58\u50a8\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u800c\u539f\u59cb\u7c7b\u578b\u7684\u503c\u5b58\u50a8\u5728\u6808\u5185\u5b58\u4e2d\u3002\u5806\u5185\u5b58\u7684\u7ba1\u7406\u6bd4\u6808\u5185\u5b58\u66f4\u590d\u6742\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><strong>\u4f55\u65f6\u4f7f\u7528\u5f15\u7528\u7c7b\u578b<\/strong><\/p>\n<p>\u5f15\u7528\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u5f53\u6211\u4eec\u9700\u8981\u5728\u591a\u4e2a\u53d8\u91cf\u4e4b\u95f4\u5171\u4eab\u503c\u65f6\u3002<\/li>\n<li>\u5f53\u6211\u4eec\u9700\u8981\u52a8\u6001\u5730\u66f4\u6539\u503c\u65f6\u3002<\/li>\n<li>\u5f53\u6211\u4eec\u9700\u8981\u5b58\u50a8\u5927\u578b\u5bf9\u8c61\u6216\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5f15l\u7528\u7c7b\u578b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>typescript \u4e2d\u7684\u5f15\u7528\u7c7b\u578b\u5141\u8bb8\u53d8\u91cf\u5f15\u7528\u53e6\u4e00\u5757\u5185\u5b58\u4e2d\u7684\u503c\uff0c\u53ef\u4f7f\u7528 any \u6216 never \u7c7b\u578b\u521b\u5efa\u3002\u5f15\u7528\u7c7b\u578b\u53ea\u80fd\u8d4b\u503c\u7ed9\u5f15\u7528\u7c7b\u578b\uff0c\u503c\u901a\u8fc7 * \u64cd\u4f5c\u7b26\u8bbf\u95ee\u3002\u5f15\u7528\u7c7b\u578b\u4e0e\u539f\u59cb\u7c7b\u578b\u4e0d\u540c\uff0c\u5b83\u5b58\u50a8\u7684\u662f\u503c\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u503c\uff0c\u5f71\u54cd\u503c\u4f20\u9012\u548c\u5185\u5b58\u7ba1\u7406\u3002 TypeScript \u4e2d\u5f15\u7528\u7c7b\u578b TypeScript \u4e2d\u7684\u5f15\u7528\u7c7b\u578b\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\uff0c\u5b83\u5c01\u88c5\u4e86\u5bf9\u53e6\u4e00\u4e2a\u7c7b\u578b\u7684\u5f15\u7528\u3002\u5f15\u7528\u7c7b\u578b\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u6216\u5c5e\u6027\u6765\u95f4\u63a5\u8bbf\u95ee\u53e6\u4e00\u5757\u5185\u5b58\u4e2d\u7684\u503c\u3002 \u521b\u5efa\u5f15\u7528\u7c7b\u578b \u8981\u521b\u5efa\u5f15\u7528\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528 any \u7c7b\u578b\u6216 never \u7c7b\u578b\u3002any \u7c7b\u578b\u8868\u793a\u53d8\u91cf\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff0c\u800c never \u7c7b\u578b\u8868\u793a\u53d8\u91cf\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8d4b\u503c\u3002\u4f8b\u5982\uff1a \/\/ any \u7c7b\u578b\u5f15\u7528 let anyReference: any; \/\/ never \u7c7b\u578b\u5f15\u7528 let neverReference: never; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u5f15\u7528\u7c7b\u578b \u5f15\u7528\u7c7b\u578b\u53ea\u80fd\u8d4b\u503c\u7ed9\u5176\u4ed6\u5f15\u7528\u7c7b\u578b\u3002\u4f8b\u5982\uff1a let reference1: any; let reference2: any = reference1; \u767b\u5f55\u540e\u590d\u5236 \u5f15\u7528\u7c7b\u578b\u7684\u503c\u901a\u8fc7\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26 * \u8bbf\u95ee\u3002\u4f8b\u5982\uff1a reference2 = 10; console.log(*reference1); \/\/ \u8f93\u51fa\uff1a10 \u767b\u5f55\u540e\u590d\u5236 [&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-3099","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3099","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=3099"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3099\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}