{"id":4876,"date":"2023-01-05T11:10:51","date_gmt":"2023-01-05T03:10:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4876"},"modified":"2023-01-05T11:10:51","modified_gmt":"2023-01-05T03:10:51","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e6%8e%a5%e5%8f%a3interfaces%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e6%8e%a5%e5%8f%a3interfaces%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u63a5\u53e3(Interfaces)\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Interfaces\"><\/p>\n<h1>TypeScript \u63a5\u53e3(Interfaces) \u4ecb\u7d39<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u6240\u6c92\u6709\u7684\u7279\u6027\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u63a5\u53e3(Interfaces)\u3002\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5b9a\u7fa9\u548c\u4f7f\u7528\u7269\u4ef6\u7684\u578b\u5225\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u6aa2\u67e5\u7a0b\u5f0f\u78bc\u7684\u578b\u5225\u5b89\u5168\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u63a5\u53e3(Interfaces) \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5b9a\u7fa9\u4e00\u500b\u7269\u4ef6\u7684\u578b\u5225\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u5c6c\u6027\u3001\u51fd\u5f0f\u3001\u7d22\u5f15\u5668\u7b49\u7b49\uff0c\u4ee5\u53ca\u5b83\u5011\u7684\u578b\u5225\u3002\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b <code>Person<\/code> \u63a5\u53e3\uff0c\u5b83\u6709\u4e00\u500b <code>name<\/code> \u5c6c\u6027\uff0c\u578b\u5225\u70ba <code>string<\/code>\uff0c\u4ee5\u53ca\u4e00\u500b <code>age<\/code> \u5c6c\u6027\uff0c\u578b\u5225\u70ba <code>number<\/code>\uff1a<\/p>\n<pre class=\"brush: typescript\">\ninterface Person {\n  name: string;\n  age: number;\n}\n<\/pre>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b\u7269\u4ef6\uff0c\u4e26\u4e14\u6307\u5b9a\u5b83\u7684\u578b\u5225\u70ba <code>Person<\/code>\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet person: Person = {\n  name: 'John',\n  age: 30\n};\n<\/pre>\n<p>\u9019\u6a23\u4e00\u4f86\uff0cTypeScript \u5c31\u6703\u6aa2\u67e5 <code>person<\/code> \u7269\u4ef6\u662f\u5426\u7b26\u5408 <code>Person<\/code> \u63a5\u53e3\u7684\u5b9a\u7fa9\uff0c\u4f8b\u5982 <code>name<\/code> \u5c6c\u6027\u662f\u5426\u70ba <code>string<\/code>\uff0c<code>age<\/code> \u5c6c\u6027\u662f\u5426\u70ba <code>number<\/code>\uff0c\u4ee5\u53ca\u662f\u5426\u6709\u5176\u4ed6\u591a\u9918\u7684\u5c6c\u6027\u3002<\/p>\n<p>\u63a5\u53e3\u4e5f\u53ef\u4ee5\u5b9a\u7fa9\u51fd\u5f0f\uff0c\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5728 <code>Person<\/code> \u63a5\u53e3\u4e2d\u5b9a\u7fa9\u4e00\u500b <code>sayHello()<\/code> \u51fd\u5f0f\uff1a<\/p>\n<pre class=\"brush: typescript\">\ninterface Person {\n  name: string;\n  age: number;\n  sayHello(): void;\n}\n<\/pre>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u5728 <code>person<\/code> \u7269\u4ef6\u4e2d\u5be6\u4f5c <code>sayHello()<\/code> \u51fd\u5f0f\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet person: Person = {\n  name: 'John',\n  age: 30,\n  sayHello() {\n    console.log(`Hello, my name is ${this.name}`);\n  }\n};\n<\/pre>\n<p>\u63a5\u53e3\u4e5f\u53ef\u4ee5\u5b9a\u7fa9\u7d22\u5f15\u5668\uff0c\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b <code>Person<\/code> \u63a5\u53e3\uff0c\u5b83\u6709\u4e00\u500b <code>name<\/code> \u5c6c\u6027\uff0c\u578b\u5225\u70ba <code>string<\/code>\uff0c\u4ee5\u53ca\u4e00\u500b <code>age<\/code> \u5c6c\u6027\uff0c\u578b\u5225\u70ba <code>number<\/code>\uff0c\u4ee5\u53ca\u4e00\u500b <code>data<\/code> \u7d22\u5f15\u5668\uff0c\u578b\u5225\u70ba <code>string<\/code>\uff1a<\/p>\n<pre class=\"brush: typescript\">\ninterface Person {\n  name: string;\n  age: number;\n  [key: string]: string;\n}\n<\/pre>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b\u7269\u4ef6\uff0c\u4e26\u4e14\u6307\u5b9a\u5b83\u7684\u578b\u5225\u70ba <code>Person<\/code>\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet person: Person = {\n  name: 'John',\n  age: 30,\n  data: '123'\n};\n<\/pre>\n<p>\u9019\u6a23\u4e00\u4f86\uff0cTypeScript \u5c31\u6703\u6aa2\u67e5 <code>person<\/code> \u7269\u4ef6\u662f\u5426\u7b26\u5408 <code>Person<\/code> \u63a5\u53e3\u7684\u5b9a\u7fa9\uff0c\u4f8b\u5982 <code>name<\/code> \u5c6c\u6027\u662f\u5426\u70ba <code>string<\/code>\uff0c<code>age<\/code> \u5c6c\u6027\u662f\u5426\u70ba <code>number<\/code>\uff0c<code>data<\/code> \u7d22\u5f15\u5668\u662f\u5426\u70ba <code>string<\/code>\uff0c\u4ee5\u53ca\u662f\u5426\u6709\u5176\u4ed6\u591a\u9918\u7684\u5c6c\u6027\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cTypeScript \u63a5\u53e3(Interfaces) \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5b9a\u7fa9\u548c\u4f7f\u7528\u7269\u4ef6\u7684\u578b\u5225\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u6aa2\u67e5\u7a0b\u5f0f\u78bc\u7684\u578b\u5225\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:TypeScript\u63a5\u53e3(Interfaces)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u63a5\u53e3\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u63a5\u53e3\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-4876","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-1gE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4876","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=4876"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4876\/revisions"}],"predecessor-version":[{"id":4877,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4876\/revisions\/4877"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}