{"id":1896,"date":"2026-05-13T15:10:23","date_gmt":"2026-05-13T07:10:23","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1896"},"modified":"2026-05-13T15:10:24","modified_gmt":"2026-05-13T07:10:24","slug":"typescript%e4%b8%adinfer%e5%85%b3%e9%94%ae%e5%ad%97%e7%9a%84%e4%bd%bf%e7%94%a8%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/05\/13\/typescript%e4%b8%adinfer%e5%85%b3%e9%94%ae%e5%ad%97%e7%9a%84%e4%bd%bf%e7%94%a8%e5%b0%8f%e7%bb%93\/","title":{"rendered":"Typescript\u4e2dinfer\u5173\u952e\u5b57\u7684\u4f7f\u7528\u5c0f\u7ed3"},"content":{"rendered":"\n<p>\u597d\u7684\uff0c\u6211\u5e2e\u4f60\u6574\u7406\u4e00\u4e2a <strong>TypeScript \u4e2d <code>infer<\/code> \u5173\u952e\u5b57\u7684\u4f7f\u7528\u5c0f\u7ed3<\/strong>\uff0c\u5e76\u9644\u4e0a\u5178\u578b\u793a\u4f8b\uff0c\u65b9\u4fbf\u5feb\u901f\u638c\u63e1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">TypeScript <code>infer<\/code> \u5173\u952e\u5b57\u5c0f\u7ed3<\/h1>\n\n\n\n<p><code>infer<\/code> \u662f TypeScript <strong>\u6761\u4ef6\u7c7b\u578b<\/strong>\u4e2d\u7528\u6765 <strong>\u63a8\u65ad\u7c7b\u578b\u53d8\u91cf<\/strong> \u7684\u5173\u952e\u5b57\uff0c\u5b83\u53ea\u80fd\u5728 <code>extends<\/code> \u5b50\u53e5\u7684\u6761\u4ef6\u7c7b\u578b\u4e2d\u4f7f\u7528\u3002\u4e3b\u8981\u7528\u9014\u662f\u4ece\u590d\u6742\u7c7b\u578b\u4e2d\u63d0\u53d6\u6216\u63a8\u5bfc\u51fa\u5b50\u7c7b\u578b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 \u57fa\u672c\u8bed\u6cd5<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype Conditional&amp;lt;T&gt; = T extends SomeType&amp;lt;infer U&gt; ? U : OtherType;\n\n<\/pre><\/div>\n\n\n<p>\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c <code>T<\/code> \u53ef\u4ee5\u8d4b\u503c\u7ed9 <code>SomeType&lt;...><\/code>\uff0c\u5c31\u628a <code>...<\/code> \u63a8\u65ad\u4e3a <code>U<\/code>\u3002<\/li>\n\n\n\n<li>\u5426\u5219\u8fd4\u56de <code>OtherType<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u5e38\u89c1\u7528\u6cd5\u793a\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\uff081\uff09\u63d0\u53d6\u6570\u7ec4\u5143\u7d20\u7c7b\u578b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype ElementType&amp;lt;T&gt; = T extends (infer U)&#x5B;] ? U : T;\n\ntype A = ElementType&amp;lt;number&#x5B;]&gt;; \/\/ number\ntype B = ElementType&amp;lt;string&gt;;   \/\/ string\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bf4\u660e\uff1a<code>infer U<\/code> \u63a8\u65ad\u6570\u7ec4\u5143\u7d20\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\uff082\uff09\u63d0\u53d6\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype ReturnType&amp;lt;T&gt; = T extends (...args: any&#x5B;]) =&gt; infer R ? R : any;\n\ntype Func = (x: number, y: number) =&gt; string;\n\ntype R = ReturnType&amp;lt;Func&gt;; \/\/ string\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>infer R<\/code> \u63a8\u65ad\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\uff083\uff09\u63d0\u53d6\u51fd\u6570\u53c2\u6570\u7c7b\u578b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype Params&amp;lt;T&gt; = T extends (...args: infer P) =&gt; any ? P : never;\n\ntype Func = (x: number, y: string) =&gt; boolean;\n\ntype P = Params&amp;lt;Func&gt;; \/\/ &#x5B;number, string]\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>infer P<\/code> \u63a8\u65ad\u51fd\u6570\u53c2\u6570\u7684\u5143\u7ec4\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\uff084\uff09\u63d0\u53d6 Promise \u5185\u90e8\u7c7b\u578b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype UnwrapPromise&amp;lt;T&gt; = T extends Promise&amp;lt;infer U&gt; ? U : T;\n\ntype P1 = UnwrapPromise&amp;lt;Promise&amp;lt;number&gt;&gt;; \/\/ number\ntype P2 = UnwrapPromise&amp;lt;string&gt;;           \/\/ string\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>infer U<\/code> \u63d0\u53d6 Promise \u7684 resolve \u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\uff085\uff09\u9012\u5f52\u7c7b\u578b\u63a8\u65ad<\/h3>\n\n\n\n<p>\u53ef\u4ee5\u7528 <code>infer<\/code> \u505a\u9012\u5f52\u7c7b\u578b\u63d0\u53d6\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype DeepArrayElement&amp;lt;T&gt; = T extends (infer U)&#x5B;]\n  ? DeepArrayElement&amp;lt;U&gt;\n  : T;\n\ntype Arr = number&#x5B;]&#x5B;]&#x5B;]; \ntype E = DeepArrayElement&amp;lt;Arr&gt;; \/\/ number\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u4ece\u591a\u7ef4\u6570\u7ec4\u4e2d\u63d0\u53d6\u6700\u5185\u5c42\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 \u4f7f\u7528\u5c0f\u6280\u5de7<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>infer<\/code> \u53ea\u80fd\u5728\u6761\u4ef6\u7c7b\u578b\u4e2d\u4f7f\u7528<\/strong>\uff1a<code>type T = infer X; \/\/ \u274c \u9519\u8bef\uff0c\u5fc5\u987b\u5728 extends \u4e2d<\/code><\/li>\n\n\n\n<li><strong>\u53ef\u642d\u914d <code>any[]<\/code> \u6216\u51fd\u6570\u7c7b\u578b\u89e3\u6784<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6570\u7ec4\uff1a<code>T extends (infer U)[]<\/code><\/li>\n\n\n\n<li>\u51fd\u6570\uff1a<code>T extends (...args: infer P) => any<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u53ef\u4ee5\u7528\u4e8e\u89e3\u6784\u5bf9\u8c61\u7c7b\u578b<\/strong>\uff08\u7ed3\u5408\u6620\u5c04\u7c7b\u578b\u548c\u6761\u4ef6\u7c7b\u578b\uff09\uff1a<code>type GetValueType&lt;T> = T extends { value: infer V } ? V : never; type Obj = { value: string }; type V = GetValueType&lt;Obj>; \/\/ string<\/code><\/li>\n\n\n\n<li><strong>\u53ef\u7528\u4e8e\u9ad8\u7ea7\u5de5\u5177\u7c7b\u578b\u7ec4\u5408<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>ReturnType&lt;T><\/code><\/li>\n\n\n\n<li><code>Parameters&lt;T><\/code><\/li>\n\n\n\n<li><code>Awaited&lt;T><\/code>\uff08TS \u5185\u7f6e\u4ece Promise \u89e3\u5305\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 \u603b\u7ed3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>infer<\/code> \u662f <strong>\u6761\u4ef6\u7c7b\u578b\u4e2d\u63d0\u53d6\u5b50\u7c7b\u578b\u7684\u5229\u5668<\/strong>\u3002<\/li>\n\n\n\n<li>\u5e38\u7528\u573a\u666f\uff1a\n<ol class=\"wp-block-list\">\n<li>\u63d0\u53d6\u6570\u7ec4\u3001Promise\u3001\u51fd\u6570\u53c2\u6570\u3001\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u505a\u9012\u5f52\u7c7b\u578b\u8ba1\u7b97\u3002<\/li>\n\n\n\n<li>\u7ed3\u5408\u5bf9\u8c61\u7c7b\u578b\u63d0\u53d6\u5b57\u6bb5\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u4e0d\u80fd\u5355\u72ec\u4f7f\u7528\uff0c\u5fc5\u987b\u5728 <code>extends<\/code> \u4e2d\u51fa\u73b0\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684\uff0c\u6211\u5e2e\u4f60\u6574\u7406\u4e00\u4e2a TypeScript \u4e2d infer \u5173\u952e\u5b57\u7684\u4f7f\u7528\u5c0f\u7ed3\uff0c\u5e76&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/05\/13\/typescript%e4%b8%adinfer%e5%85%b3%e9%94%ae%e5%ad%97%e7%9a%84%e4%bd%bf%e7%94%a8%e5%b0%8f%e7%bb%93\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1896","post","type-post","status-publish","format-standard","hentry","category-1"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1896"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1896\/revisions"}],"predecessor-version":[{"id":1897,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1896\/revisions\/1897"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}