{"id":334,"date":"2022-01-05T15:08:39","date_gmt":"2022-01-05T07:08:39","guid":{"rendered":"http:\/\/gjweb.top\/?p=334"},"modified":"2022-01-05T15:08:40","modified_gmt":"2022-01-05T07:08:40","slug":"4-ts%e8%bf%9b%e9%98%b6%e7%af%87","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=334","title":{"rendered":"4. TS\u8fdb\u9636\u7bc7"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u7c7b\u578b\u65ad\u8a00<\/li><li>\u679a\u4e3e\u7c7b\u578b<\/li><li>\u6cdb\u578b<ol start=\"\"><li>\u51fd\u6570\u4e2d\u4f7f\u7528\u6cdb\u578b<\/li><li>\u7c7b\u4e2d\u4f7f\u7528\u6cdb\u578b<\/li><li>\u6cdb\u578b\u4e2dkeyof\u4f7f\u7528<\/li><\/ol><\/li><li>namespace\u547d\u540d\u7a7a\u95f4<\/li><li>\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7c7b\u578b\u65ad\u8a00<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Bird {<br> &nbsp;fly:boolean,<br> &nbsp;flyFun:()=&gt;{}<br>}<br>interface Dog {<br> &nbsp;fly:boolean,<br> &nbsp;dogFun:()=&gt;{}<br>}<br>\/\/ \u7c7b\u578b\u65ad\u8a00<br>function animal(params:Bird|Dog){<br> &nbsp;if(params.fly){<br> &nbsp;  (params as Bird).flyFun()<br>  }else{<br> &nbsp;  (params as Dog).dogFun()<br>  }<br>}<br>\/\/ \u7c7b\u578b\u4fdd\u62a4 in \u8bed\u6cd5<br>function animal2(params:Bird|Dog){<br> &nbsp;if(\"flyFun\" in params){<br> &nbsp; &nbsp;params.flyFun()<br>  }else{<br> &nbsp; &nbsp;params.dogFun()<br>  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u679a\u4e3e\u7c7b\u578b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Result {<br> &nbsp;one,<br> &nbsp;tow,<br> &nbsp;three<br>}<br>console.log(Result.one) \/\/ 0<br>console.log(Result.tow) \/\/ 1<br>console.log(Result.three) \/\/2<br>console.log(Result&#91;1]) \/\/ tow<br>enum Result {<br> &nbsp;one,<br> &nbsp;tow=2,<br> &nbsp;three<br>}<br>console.log(Result.one) \/\/ 0<br>console.log(Result.tow) \/\/ 2<br>console.log(Result.three) \/\/3 <br>console.log(Result&#91;2]) \/\/ tow<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6cdb\u578b<\/h2>\n\n\n\n<p>\u6cdb\u578b\u7b80\u5355\u7406\u89e3\u4e3a \u6cdb\u6307\u7684\u7c7b\u578b(generic)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u51fd\u6570\u4e2d\u4f7f\u7528\u7c7b\u578b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u7b80\u5355\u7528\u6cd5<br>function join&lt;T&gt;(first:T,last:T){<br> &nbsp; &nbsp;return `${first}${last}`<br>}<br>join&lt;string&gt;(\"hllo\",\"world\"); \/\/ \"hello world\"<br>\/\/ \u7075\u6d3b\u6cdb\u578b<br>function map&lt;T&gt;(arr:Array&lt;T&gt;){<br> &nbsp; &nbsp;console.log(arr)<br>}<br>map(&lt;string|number|{&#91;propName:string]:any}&gt;)(&#91;123,\"123\",{name:\"this a object\"}])<br>\/\/ \u591a\u91cd\u6cdb\u578b<br>function sub&lt;T,P&gt;(first:T,last:P){<br> &nbsp; &nbsp;console.log(first,last)<br>}<br>sub&lt;number,string&gt;(1,\"string\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7c7b\u4e2d\u4f7f\u7528\u7c7b\u578b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface item{<br> &nbsp;name:string<br>} <br>class Xxx&lt;T extends item&gt;{<br> &nbsp;constructor(private data:T&#91;]){}<br> &nbsp;getItem(index:number):string{<br> &nbsp; &nbsp;return this.data&#91;index].name<br>  }<br>}<br>let xxx = new Xxx(&#91;<br>  {<br> &nbsp; &nbsp;name:\"hello\"<br>  }<br>])<br>console.log(xxx.getItem(0))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">namespace \u547d\u540d\u7a7a\u95f4<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Home { \/\/ namespace \u4f1a\u751f\u6210\u5168\u5c40\u53d8\u91cf Home<br> &nbsp; &nbsp;export class CreateHtml{<br> &nbsp; &nbsp; &nbsp; &nbsp;constructor(){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<br>declare var $ : (param:()=&gt;void) = &gt; void<br>\u200b<br>\/\/ \u5b9a\u4e49\u5168\u5c40\u51fd\u6570 &amp; \u51fd\u6570\u91cd\u8f7d <br>declare function $(params:()=&gt;void):void;<br>declare function $(param:string):{html:(html:string)=&gt;{}} <\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6cdb\u578b\u4e2dkeyof\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Person {<br> &nbsp; &nbsp;name:string,<br> &nbsp; &nbsp;age:number,<br> &nbsp; &nbsp;gender:string<br>}<br>class Teacher{<br> &nbsp; &nbsp;constructor(private info:Person){}<br> &nbsp; &nbsp;getInfo&lt;T extends keyof Person&gt;(key:T): Person&#91;T]{<br> &nbsp; &nbsp; &nbsp; &nbsp;return this.info&#91;key];<br> &nbsp;  }<br>}<br>const tercher = new Thacher({<br> &nbsp; &nbsp;name:'dell',<br> &nbsp; &nbsp;age:18,<br> &nbsp; &nbsp;gender:\"male\"<br>})<br>const test = teacher.getInfo(\"hello\")<br>console.log(test)<br>\u200b<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u578b\u65ad\u8a00 \u679a\u4e3e\u7c7b\u578b \u6cdb\u578b \u51fd\u6570\u4e2d\u4f7f\u7528\u6cdb\u578b \u7c7b\u4e2d\u4f7f\u7528\u6cdb\u578b \u6cdb\u578b\u4e2dkeyof\u4f7f\u7528 namespace\u547d\u540d\u7a7a\u95f4 \u5b9a\u4e49 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-typescript"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=334"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}