{"id":322233,"date":"2021-02-05T15:39:09","date_gmt":"2021-02-05T07:39:09","guid":{"rendered":"http:\/\/4563.org\/?p=322233"},"modified":"2021-02-05T15:39:09","modified_gmt":"2021-02-05T07:39:09","slug":"%e7%94%a8-typescript-%e5%86%99%e4%ba%86%e4%b8%aa%e5%87%bd%e6%95%b0%ef%bc%8c%e9%a2%91%e9%a2%91%e6%8a%a5%e9%94%99%ef%bc%8c%e8%af%b7%e6%95%99%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=322233","title":{"rendered":"\u7528 typescript \u5199\u4e86\u4e2a\u51fd\u6570\uff0c\u9891\u9891\u62a5\u9519\uff0c\u8bf7\u6559\u5982\u4f55\u89e3\u51b3"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7528 typescript \u5199\u4e86\u4e2a\u51fd\u6570\uff0c\u9891\u9891\u62a5\u9519\uff0c\u8bf7\u6559\u5982\u4f55\u89e3\u51b3               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : november <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>typescript \u521d\u5b66\u8005\u5f00\u59cb\u7528 typescript \u8bd5\u7740\u5199\u70b9\u4e1c\u897f\uff0c\u5374\u5404\u79cd\u62a5\u9519\u3002\u9042\u6765\u8bf7\u6559\u5404\u4f4d\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u51fd\u6570\u4f5c\u7528\u662f\u5c06\u4e24\u4e2a\u5bf9\u8c61\u5408\u5e76\uff0c\u5e76\u8fd4\u56de\u5408\u5e76\u7ed3\u679c\u3002<\/p>\n<pre><code>function _merge&lt;T extends U, U&gt; (target: T, source: U) : T {   for (let key in source) {     if (Object.prototype.hasOwnProperty.call(source, key)) {       const value = source[key]       if (isObject(target[key]) &amp;&amp; isObject(value)) {         target[key] = _merge(target[key], value)       } else if (isObject(value)) {         target[key] = _merge({}, value)       } else if (isArray(value)) {         target[key] = _merge([], value)       }     }   }   return target } <\/code><\/pre>\n<p>\u7136\u540e\u5728<code>target[key] = _merge({}, value)<\/code>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u7684<code>target[key]<\/code>\u7684\u9519\u8bef\u662f<\/p>\n<pre><code>Type 'U[Extract&lt;keyof U, string&gt;]' is not assignable to type 'T[Extract&lt;keyof U, string&gt;]'.  Type 'U' is not assignable to type 'T'.   'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.ts(2322) <\/code><\/pre>\n<p>\u5728 <code>{}<\/code> \u53c8\u6709\u9519\u8bef <\/p>\n<p><code>Argument of type '{}' is not assignable to parameter of type 'U[Extract&lt;keyof U, string&gt;]'.ts(2345)<\/code><\/p>\n<p>\u6700\u540e\u5728 <code>target[key] = _merge([], value)<\/code> \u5728\u7684 <code>[]<\/code> \u4e5f\u6709\u95ee\u9898 <\/p>\n<p><code>Argument of type 'undefined[]' is not assignable to parameter of type 'never'.ts(2345)<\/code><\/p>\n<p>\u8bf7\u6c42\u5982\u4f55\u89e3\u51b3\u4e0a\u9762\u7684\u95ee\u9898\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5202136\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : november <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6c42\u4e2a\u5927\u4f6c\u7ed9\u4e2a\u89e3\u51b3\u601d\u8def\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202137\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BingoXuan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8981\u4e0d\u770b\u4e00\u4e0b Object.assign \u7684\u7b7e\u540d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202138\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Justin13 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ts-ignore                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202139\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : november <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BingoXuan \u8bf7\u95ee\u600e\u4e48\u67e5\u770b\u7b7e\u540d\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202140\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BingoXuan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @november <br \/>\u6211\u5728 webstorm \u91cc\u9762\u5bf9\u5e94\u7684\u7b7e\u540d\u6765\u81ea https:\/\/github.com\/microsoft\/TypeScript\/blob\/master\/lib\/lib.es2015.core.d.ts<br \/>\u7b7e\u540d\u662f assign&lt;T, U&gt;(target: T, source: U): T &amp; U;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202141\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SxqSachin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u7b49\u5927\u4f6c\u56de\u7b54\uff0c\u6211\u81ea\u5df1\u5199\u8fd9\u7c7b\u65b9\u6cd5\u7684\u65f6\u5019\u662f\u6700\u540e\u8fd4\u56de\u7684\u65f6\u5019\u5e26\u4e00\u4e2a as T                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202142\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sillydaddy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u52a0\u4e00\u4e2a as any\uff0c\u5c31\u53ef\u4ee5\u89e3\u51b3\uff1a<br \/>target[key] = _merge({} as any, value)<br \/>target[key] = _merge([] as any, value)<\/p>\n<p>\u867d\u7136\u53d6\u5de7(\u56e0\u4e3a\u6ca1\u6709\u660e\u786e\u7c7b\u578b)\uff0c\u4f46\u56e0\u4e3a\u663e\u800c\u6613\u89c1\uff0c\u6240\u4ee5\u4e0d\u4f1a\u9020\u6210\u9519\u8bef\u3002\u5426\u5219\u9700\u8981\u597d\u597d\u5543\u4e00\u5543 TypeScript \u7684\u7c7b\u578b\u7cfb\u7edf\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202143\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mxT52CRuqR6o5 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5199\u4e0d\u51fa\u7c7b\u578b\u5b89\u5168\u7684\u5f88\u6b63\u5e38\uff0c\u4f60\u8fd9\u79cd\u60c5\u51b5\u5e94\u8be5\u5c31\u662f\u5199\u4e0d\u51fa\u6765\u7684\uff0c\u7528\u5404\u79cd any \u7ed5\u8fc7\u53bb\u5c31\u884c\u4e86<br \/>\u5176\u4ed6\u5f3a\u7c7b\u578b\u8bed\u8a00\u57fa\u672c\u90fd\u5199\u4e0d\u51fa\u8fd9\u79cd\u901a\u7528\u7684 merge \u51fd\u6570\u5427\uff0cjava \u5982\u679c\u7528\u53cd\u5c04\u6765\u5b9e\u73b0\u4fdd\u8bc1\u4e0d\u4e86\u7c7b\u578b\u5b89\u5168\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202144\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chenluo0429 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u867d\u7136\u7528\u4e86 typescript\uff0c\u4f46\u662f\u4f60\u7684\u4ee3\u7801\u5b9e\u9645\u4e0a\u4e00\u70b9\u90fd\u4e0d type \u3002_merge \u7684\u53c2\u6570\u8981\u6c42 target \u7684\u7c7b\u578b\u4e3a source \u7684\u5b50\u7c7b\uff0c\u4f46\u662f_merge({}, value)\u4e2d{}\u600e\u4e48\u4fdd\u8bc1\u662f value \u7684\u5b50\u7c7b\uff1f\u4f60\u5199\u4e86\u4e0d\u7b26\u5408 type \u7684\u810f\u4ee3\u7801\uff0c\u8981\u4e48 ts-ignore\uff0c\u8981\u4e48\u5c31\u4e0a any                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202145\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : november <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BingoXuan <br \/>\u6211\u7528\u7684 vscode\uff0c\u6211\u60f3\u5e94\u8be5\u662f\u4e00\u6837\u7684\u3002\u4e0d\u8fc7\u6211\u8fd9\u91cc\u4e0d\u662f\u51fd\u6570\u58f0\u660e\u7684\u65f6\u5019\u62a5\u9519\uff0c\u800c\u662f\u5185\u90e8\u5b9e\u73b0\u65f6\uff0c\u8d4b\u503c\u62a5\u9519\u3002<\/p>\n<p>@sillydaddy @mxT52CRuqR6o5 <br \/>\u5bf9\u8c61\u90a3\u884c\u7528 any \u53ef\u4ee5\u7ed5\u8fc7\u53bb\uff0c\u4f46\u662f\u6570\u7ec4\u90a3\u884c\u8fd8\u662f\u62a5\u9519\u8bf4\uff0c\u4e0d\u80fd\u8d4b\u503c\u7ed9 nerver \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202146\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : november <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chenluo0429 <\/p>\n<p>\u8bf4\u5f97\u6709\u70b9\u9053\u7406\uff0c\u90a3\u6211\u6539\u6210\u4e86\u8fd9\u6837\u5b50\uff1a_merge&lt;T, U&gt; (target: T, source: U) : (T &amp; U)<\/p>\n<p>\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u6bcf\u6b21\u8bbf\u95ee T[key] \u7684\u65f6\u5019\uff0c\u9664\u975e\u8fdb\u884c\u65ad\u8a00 (&lt;T&amp;U&gt;T)[key]\uff0c\u5426\u5219\u90fd\u62a5\u9519\u3002<br \/>Type &#8216;Extract&lt;keyof U, string&gt;&#8217; cannot be used to index type &#8216;T&#8217;.ts(2536)<\/p>\n<p>\u53e6\u5916\uff0c(&lt;T&amp;U&gt;target)[key] = _merge({}, value)\u8fd9\u4e00\u884c\uff0c\u5bf9\u4e8e(&lt;T&amp;U&gt;target)[key]\u4f9d\u7136\u6709\u9519\u8bef\u3002<\/p>\n<p>Type &#8216;{} &amp; U[Extract&lt;keyof U, string&gt;]&#8217; is not assignable to type &#8216;(T &amp; U)[Extract&lt;keyof U, string&gt;]&#8217;.ts(2322)<\/p>\n<p>\u8bf7\u95ee\u8fd9\u4e00\u884c\u600e\u4e48\u5f04\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202147\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mxT52CRuqR6o5 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @november <br \/>\u56e0\u4e3a array \u4e5f\u662f object\uff0c\u4f60\u8fd9\u6761\u5206\u652f\u662f\u8d70\u4e0d\u5230\u7684\uff0c\u7c7b\u578b\u5c31\u6210\u4e86 never                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5202148\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : november <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mxT52CRuqR6o5 <br \/>\u554a\uff0c\u662f\uff0c\u4f60\u8bf4\u5f97\u5bf9\u3002\u5e73\u65f6\u6478\u9c7c\u592a\u591a\u4e86\uff0c\u4e00\u4e9b\u57fa\u672c\u77e5\u8bc6\u90fd\u5ffd\u7565\u4e86\u3002\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 typescript \u5199\u4e86\u4e2a\u51fd&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\/322233"}],"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=322233"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/322233\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}