{"id":5010,"date":"2023-01-05T11:37:15","date_gmt":"2023-01-05T03:37:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5010"},"modified":"2023-01-05T11:37:15","modified_gmt":"2023-01-05T03:37:15","slug":"%e5%ad%b8%e7%bf%92typescript%e6%8e%a5%e5%8f%a3%e4%b8%ad%e7%9a%84%e5%8f%aa%e8%ae%80%e5%b1%ac%e6%80%a7","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92typescript%e6%8e%a5%e5%8f%a3%e4%b8%ad%e7%9a%84%e5%8f%aa%e8%ae%80%e5%b1%ac%e6%80%a7\/","title":{"rendered":"\u5b78\u7fd2TypeScript\u63a5\u53e3\u4e2d\u7684\u53ea\u8b80\u5c6c\u6027"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u63a5\u53e3, \u53ea\u8bfb\u5c5e\u6027\"><\/p>\n<h1>TypeScript \u63a5\u53e3(interfaces)\u7684\u53ea\u8bfb\u5c5e\u6027<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e\u7531\u5fae\u8edf\u958b\u767c\u7684 JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u985e\u578b\u7cfb\u7d71\u548c\u5c0d ES6 \u7684\u652f\u6301\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u7a0b\u5f0f\u78bc\u3002TypeScript \u63a5\u53e3(interfaces)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u5c0d\u8c61\u7684\u7d50\u69cb\uff0c\u4e26\u4e14\u53ef\u4ee5\u8a2d\u5b9a\u5c6c\u6027\u7684\u53ea\u8b80\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>TypeScript \u63a5\u53e3(interfaces)\u7684\u53ea\u8b80\u5c6c\u6027\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u597d\u5730\u63a7\u5236\u5c0d\u8c61\u7684\u5c6c\u6027\uff0c\u53ef\u4ee5\u9632\u6b62\u5c0d\u8c61\u7684\u5c6c\u6027\u88ab\u610f\u5916\u5730\u66f4\u6539\u3002\u53ea\u8b80\u5c6c\u6027\u53ef\u4ee5\u4f7f\u7528 <code>readonly<\/code> \u4fee\u98fe\u8a5e\u4f86\u5b9a\u7fa9\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\ninterface Point {\n    readonly x: number;\n    readonly y: number;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b Point \u63a5\u53e3\uff0c\u5b83\u6709\u5169\u500b\u53ea\u8b80\u5c6c\u6027 x \u548c y\u3002\u53ea\u8b80\u5c6c\u6027\u53ea\u80fd\u5728\u5b9a\u7fa9\u6642\u6216\u8005\u69cb\u9020\u51fd\u6578\u4e2d\u88ab\u8ce6\u503c\uff0c\u4e0d\u80fd\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u66f4\u6539\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet point: Point = { x: 10, y: 20 };\npoint.x = 5; \/\/ \u932f\u8aa4\uff1ax \u662f\u53ea\u8b80\u5c6c\u6027\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u5617\u8a66\u5c07 Point \u5c0d\u8c61\u7684 x \u5c6c\u6027\u66f4\u6539\u70ba 5\uff0c\u4f46\u662f\u7531\u65bc x \u662f\u53ea\u8b80\u5c6c\u6027\uff0c\u6240\u4ee5\u6703\u5831\u932f\u3002<\/p>\n<p>\u53ea\u8b80\u5c6c\u6027\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u5c0d\u8c61\u7684\u5c6c\u6027\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f86\u5b9a\u7fa9\u9663\u5217\u7684\u5143\u7d20\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet a: number[] = [1, 2, 3, 4];\na[0] = 10; \/\/ \u6b63\u78ba\na.push(5); \/\/ \u6b63\u78ba\na.length = 100; \/\/ \u6b63\u78ba\na = [1, 2, 3, 4]; \/\/ \u932f\u8aa4\uff1aa \u662f\u53ea\u8b80\u7684\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u53ea\u8b80\u7684\u9663\u5217 a\uff0c\u5b83\u7684\u5143\u7d20\u53ea\u80fd\u88ab\u8b80\u53d6\uff0c\u4e0d\u80fd\u88ab\u66f4\u6539\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cTypeScript \u63a5\u53e3(interfaces)\u7684\u53ea\u8b80\u5c6c\u6027\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u597d\u5730\u63a7\u5236\u5c0d\u8c61\u7684\u5c6c\u6027\uff0c\u53ef\u4ee5\u9632\u6b62\u5c0d\u8c61\u7684\u5c6c\u6027\u88ab\u610f\u5916\u5730\u66f4\u6539\uff0c\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2TypeScript\u63a5\u53e3\u4e2d\u7684\u53ea\u8b80\u5c6c\u6027\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u597d\u5730\u63a7\u5236\u8cc7\u6599\u7684\u6d41\u52d5\uff0c\u4e26\u78ba\u4fdd\u8cc7\u6599\u7684\u5b89\u5168\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u63a5\u53e3\u4e2d\u7684\u53ea\u8b80\u5c6c\u6027\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5010","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1iO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5010"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5010\/revisions"}],"predecessor-version":[{"id":5011,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5010\/revisions\/5011"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}