{"id":602,"date":"2017-12-27T17:57:37","date_gmt":"2017-12-27T09:57:37","guid":{"rendered":"https:\/\/sora.sound.moe\/?p=602"},"modified":"2017-12-27T17:57:37","modified_gmt":"2017-12-27T09:57:37","slug":"typescript%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%9a%e6%b3%9b%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/sora.sound.moe\/?p=602","title":{"rendered":"TypeScript\u5b66\u4e60\u7b14\u8bb0\uff1a\u6cdb\u578b"},"content":{"rendered":"<p>\u53ea\u662f\u5b66\u4e60\u7b14\u8bb0\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u9047\u5230\u590d\u6742\u7684\u7c7b\u578b\u5b9a\u4e49\uff0c\u5c31\u5f97\u7528TypeScript \u4e2d\u7684\u6cdb\u578b\u4e86\u3002<\/p>\n<p>\u5176\u5b9e\u6cdb\u578b\u53ef\u4ee5\u8bf4\u662f\u7c7b\u578b\u7684\u53d8\u91cf\u3002\u4ece\u6700\u7b80\u5355\u7684\u4f8b\u5b50\u770b\u8d77<\/p>\n<pre class=\"lang:js decode:true\">function identity&lt;T&gt;(arg: T): T {\n    return arg;\n}<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u53ea\u662f\u60f3\u8868\u8fbe\u4e00\u4e0b\uff0c\u8f93\u5165\u5565\u7c7b\u578b\u5c31\u8fd4\u56de\u5565\u7c7b\u578b\u3002\u7136\u800c\u8f93\u5165\u7c7b\u578b\u53ef\u80fd\u6709\u5f88\u591a\u79cd\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u4e00\u4e2a\u6cdb\u578b\uff0cT\u662f\u4e00\u4e2a\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/p>\n<pre class=\"lang:js decode:true \">function identity&lt;T&gt;(arg: T[]): T {\n    return arg[1];\n}<\/pre>\n<p>\u8fd9\u4e2a\u53ef\u4ee5\u66f4\u76f4\u89c2\u770b\u51faT\u662f\u4e2a\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u8f93\u5165T\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u8f93\u51fa\u5176\u4e2d\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u8c03\u7528\u65f6\uff0c\u53ef\u4ee5\u7ed9\u6cdb\u578b\u4f20\u9012\u7c7b\u578b\u3002<\/p>\n<pre class=\"lang:js decode:true \">identity&lt;string&gt;(['1']);<\/pre>\n<p>\u4f46\u6ca1\u6709\u5fc5\u8981\uff0cts\u53ef\u4ee5\u81ea\u52a8\u63a8\u5bfc\u51fa\u7c7b\u578b\u3002<\/p>\n<pre class=\"lang:js decode:true \">identity(['1']);<\/pre>\n<p>\u5f53\u51fd\u6570\u7684\u7c7b\u578b\u6bd4\u8f83\u590d\u6742\u7684\u65f6\u5019\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6cdb\u578b\u63a5\u53e3<\/p>\n<pre class=\"lang:js decode:true \">interface GenericIdentityFn {\n    &lt;T&gt;(arg: T): T;\n}<\/pre>\n<p>\u968f\u540e\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u5c31\u597d\u8bf4\u4e86<\/p>\n<pre class=\"lang:js decode:true\">function identity&lt;T&gt;(arg: T): T {\n    return arg;\n}\n\nlet myIdentity: GenericIdentityFn = identity;<\/pre>\n<p>\u5b9a\u4e49\u4e86\u6cdb\u578b\u63a5\u53e3\u4e4b\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u89c4\u5b9a\u5177\u4f53\u53ef\u4ee5\u4f20\u5165\u54ea\u4e9b\u73a9\u610f\u4e86<\/p>\n<p>\u7ed9\u8fd9\u4e2a\u63a5\u53e3\u52a0\u4e2a\u6cdb\u578b<\/p>\n<pre class=\"lang:js decode:true\">interface GenericIdentityFn&lt;T&gt; {\n    (arg: T): T;\n}<\/pre>\n<p>\u8fd9\u6837T\u5c31\u4ece\u201c\u5b9e\u73b0\u6b64\u63a5\u53e3\u7684\u51fd\u6570\u7684\u4f20\u5165\u7c7b\u578b\u201d\u53d8\u6210\u4e86\u201c\u5b9e\u73b0\u63a5\u53e3\u65f6\u89c4\u5b9a\u7684\u7c7b\u578b\u201d<\/p>\n<pre class=\"lang:js decode:true \">const myIdentity: GenericIdentityFn&lt;string | number&gt; = (arg: string) =&gt; arg;<\/pre>\n<p>\u8fd9\u91cc\u89c4\u5b9a\uff0c\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u7684\u51fd\u6570\u5fc5\u987b\u63a5\u6536\u5b57\u7b26\u4e32\u6216\u8005\u6570\u5b57\u3002<\/p>\n<p>\u8d4b\u503c\u7684\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u7684\u662f\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u7c7b\u578b\u68c0\u67e5\u901a\u8fc7\u3002<\/p>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b\u6765\u5bf9\u4f20\u5165\u53c2\u6570\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u9650\u5236\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ea\u8981\u6c42\u4f20\u5165\u7684\u8fd9\u4e2a\u53c2\u6570\u62e5\u6709length\u7c7b\u578b\uff0c\u800c\u4e0d\u7ba1\u662f\u6570\u7ec4\u8fd8\u662f\u5b57\u7b26\u4e32\u8fd8\u662f\u7c7b\u6570\u7ec4\u7684\u73a9\u610f\u3002<\/p>\n<pre class=\"lang:js decode:true\">interface Lengthwise {\n    length: number;\n}\n\nfunction loggingIdentity&lt;T extends Lengthwise&gt;(arg: T): T {\n    console.log(arg.length);\n    return arg;\n}<\/pre>\n<p>\u66f4\u8fdb\u9636\u7684\uff0c\u4f8b\u5982\u8981\u505a\u4e00\u4e2a\u4eceobject\u4e2d\u53d6\u503c\u7684\u51fd\u6570\uff0c\u6211\u4eec\u60f3\u8981key\u5fc5\u987b\u662f\u8fd9\u4e2aobject\u4e2d\u5b58\u5728\uff0c\u8fd9\u6837\u7684\u9700\u6c42\u7adf\u7136\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u3002<\/p>\n<pre class=\"lang:js decode:true \">function getProperty&lt;T, K extends keyof T&gt;(obj: T, key: K) {\n    return obj[key];\n}\n\nlet x = { a: 1, b: 2, c: 3, d: 4 };\n\ngetProperty(x, \"a\"); \/\/ okay\ngetProperty(x, \"m\"); \/\/ error: Argument of type 'm' isn't assignable to 'a' | 'b' | 'c' | 'd'.<\/pre>\n<p><a href=\"https:\/\/zhongsp.gitbooks.io\/typescript-handbook\/content\/doc\/handbook\/Generics.html\">https:\/\/zhongsp.gitbooks.io\/typescript-handbook\/content\/doc\/handbook\/Generics.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ea\u662f\u5b66\u4e60\u7b14\u8bb0\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"twentyseventeen-featured-image":false,"twentyseventeen-thumbnail-avatar":false},"uagb_author_info":{"display_name":"Sora","author_link":"https:\/\/sora.sound.moe\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":"\u53ea\u662f\u5b66\u4e60\u7b14\u8bb0\u3002","_links":{"self":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/602"}],"collection":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=602"}],"version-history":[{"count":0,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions"}],"wp:attachment":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}