{"id":433048,"date":"2021-04-18T10:39:20","date_gmt":"2021-04-18T02:39:20","guid":{"rendered":"http:\/\/4563.org\/?p=433048"},"modified":"2021-04-18T10:39:20","modified_gmt":"2021-04-18T02:39:20","slug":"%e6%b1%82%e5%8a%a9%ef%bc%8c%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa-typescript-%e7%b1%bb%e5%9e%8b%e5%86%99%e6%b3%95%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=433048","title":{"rendered":"\u6c42\u52a9\uff0c\u8bf7\u6559\u4e00\u4e2a Typescript \u7c7b\u578b\u5199\u6cd5\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u52a9\uff0c\u8bf7\u6559\u4e00\u4e2a Typescript \u7c7b\u578b\u5199\u6cd5\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : usedtobe <\/span>  <span><i><\/i> 12<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>ts \u5c0f\u767d\uff0c\u521d\u6b21\u5b66\u4e60 ts\uff0c\u60f3\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\uff0c\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u8fd4\u56de\u4e00\u4e2a\u4ee5\u8fd9\u4e2a\u6570\u7ec4\u91cc\u7684\u5143\u7d20\u4f5c\u4e3a key \u7684\u5bf9\u8c61<\/p>\n<p>function fn (array: string[]): ReturnType {}<\/p>\n<p>\u6bd4\u5982\u4f20\u5165 [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]\uff0c\u4f1a\u8fd4\u56de {&#8216;a&#8217;: 1, &#8216;b&#8217;: 2, &#8216;c&#8217;: 3}<\/p>\n<p>\u73b0\u5728\u5361\u5728\u4e86\u8fd9\u4e2a ReturnType \u7c7b\u578b\u8981\u600e\u4e48\u5199\u4e0a\u9762\u3002\u3002\u3002<\/p>\n<p>\u4e00\u79cd\u601d\u8def\u662f<\/p>\n<p>interface ReturnType { [key: string]: number }<\/p>\n<p>\u4f46\u662f\u8fd9\u6837\u63d0\u793a\u4e0d\u591f\u7cbe\u786e\uff0c\u60f3\u8ba9 ts \u80fd\u591f\u63d0\u793a\u51fa\u8fd4\u56de\u503c\u5305\u542b a,b,c \u4e09\u4e2a\u5c5e\u6027\uff0c\u5927\u5bb6\u6709\u4ec0\u4e48\u601d\u8def\u5417<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5639242\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cuvii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             interface IType {<br \/> a: number;<br \/> b: number;<br \/> c: number;<br \/>}<\/p>\n<p>\u8fd9\u6837\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5639243\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             declare function fn&lt;T extends string&gt;(input: T[]): Record&lt;T, number&gt;;<\/p>\n<p>const ret = fn([&#8216;a&#8217;, &#8216;b&#8217;]);<br \/>ret.a = 1;<br \/>ret.b = 2;<br \/>ret.c = 3; \/\/ error: Property &#8216;c&#8217; does not exist on type &#8216;Record&lt;&#8220;a&#8221; | &#8220;b&#8221;, number&gt;&#8217;.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5639244\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cuvii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @codehz \u5e94\u8be5\u662f\u4f60\u8fd9\u6837&#8230;\u6211\u6ca1\u770b\u5165\u53c2&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5639245\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : noe132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/www.typescriptlang.org\/play?target=99#code\/C4TwDgpgBAggTnAKgewEoQMbLgEwDyJQQAewEAdjgM5RwQCGOy5ANiFFcHAJbkDmAbQC6AGiioipCtXGZs+Tj35jyAVwC2AIwhwAfFAC8UAN4BffUcIkylGgIB0j3gDMdUAIpiXbgOpCAUFBBUAD8UD6SNjKKvHxQgcGJYe6R0jQx-MIJiTmhJlACANZQvOFCAFweAgDkLBR8wAAW1UJQplAAZLAIKOhYuHjuutm5wZWoI4njI9P+oJBQAGIs9MA2AKrk3MwEFiYjRSXkUIUQIMjOUIgVV0UBpv7+WOScUM8AbjrAhlAEqbYcLixYS6AAU9AQlUQAEpDPpjCNnq9kJoAFaVVTkQrkZAAd2ORgA8mjMMB7M44Mh1ABRchAiBUcEIezqehgUGg95eWEGfQCLklITQ6EjOjAVRwY4o1FQeg0ZarDZbHbwJBoOQDRC6YYPJ7MV50KiqFjfIwfL6gmr0apiaqaG1QaoYFqyuzVa22+2251Cx5I5B1ewsZB8UGG41k+iGAxGAAMIv9geDofDJvsmmjRgAjAn9QGIEGQ2GGRH7BhM1AAEzQoA                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5639246\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : noe132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5ffd\u7565\u4e0a\u9762 1 \u3002\u8fd9\u4e2a\u5e94\u8be5\u5c31\u6ca1\u95ee\u9898\u4e86\u3002<br \/>const result = convert([&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;] as const)<br \/>\u9700\u8981\u7528 as const\uff0c\u8fd9\u6837 ts \u5c31\u80fd\u62ff\u5230 arr \u5177\u4f53\u7684\u7c7b\u578b\u4e86\u3002<\/p>\n<p>https:\/\/www.typescriptlang.org\/play?target=99&amp;ssl=1&amp;ssc=1&amp;pln=22&amp;pc=28#code\/C4TwDgpgBAggTnAKgewEoQMbLgEwDyJQQAewEAdjgM5RwQCGOy5ANiFFcHAJbkDmAbQC6AGiioipCtXGZs+Tj35jyAVwC2AIwhwAfFAC8UAN4BffUcIkylGnUbM2UAQDo3vAGY6oARTGfvAHUhACgocKgAfihAyRsZRV4+KDCItOifOOkaRP5hVLTCqJNnAGsoXhihAC5fAQByFgo+YAALeqEoUygAMlgEFHQsXDwfXQKiiNrUCbTpifmQ0EgoADEWemAbAFVybmYCCxMJgXLK0ogQZA8oRBrb09DTEJCsck4oN4A3HWBDKAIWVstAYTFY7FygiEugAFPQELVEABKQz6YwTN4fZCaABWtVU5FK5GQAHdyP8APK4zDAFweODIdQAUXIXG4ECocIQLnU9DAMJhX38KIM+gEQoqQiRSImdGAqjg5OxOKg9Bo602Oz2B3gSDQchGiF042er2YHzoVFULD+Rm+vxhDXo9TE9U0Lqg9QwHVVNExwBlZveyCaLhYyD4MMt1tp9EMBiMAAYZZiQxAwxGoxyYy5NPGjABGFPmtMZyPRm0uDD5qAAJiRQA                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5639247\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @noe132 \u4e0d\u662f\u5f88\u61c2\uff0c&lt;T extends string&gt;(input: T[]): Record&lt;T, number&gt;\u80fd\u89e3\u51b3\u7684\u4e8b\u60c5\u4e3a\u5565\u8981\u73a9\u7c7b\u578b\u4f53\u64cd                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5639248\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : noe132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @codehz \u4ee5\u4e3a\u4ed6\u8981\u8fde\u503c\u7684\u7c7b\u578b\u4e5f\u8981\u4e00\u8d77\u6709\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u52a9\uff0c\u8bf7\u6559\u4e00\u4e2a Typescrip&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/433048"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=433048"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/433048\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=433048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=433048"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=433048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}