<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>泛型 | 锤子前端</title><description>Now vibe coding, so learning hammer FE ?</description><link>https://channel.meo-ai.com</link><item><title>《TypeScript类型变量推断机制深度解析》</title><link>https://channel.meo-ai.com/posts/659</link><guid isPermaLink="true">https://channel.meo-ai.com/posts/659</guid><pubDate>Thu, 11 Jun 2026 15:25:03 GMT</pubDate><content:encoded>&lt;span class=&quot;post-title-source&quot;&gt;《TypeScript类型变量推断机制深度解析》&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;标签：&lt;a href=&quot;/search/result?q=%23TypeScript&quot; title=&quot;#TypeScript&quot;&gt;#TypeScript&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E7%B1%BB%E5%9E%8B%E7%B3%BB%E7%BB%9F&quot; title=&quot;#类型系统&quot;&gt;#类型系统&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E7%B1%BB%E5%9E%8B%E6%8E%A8%E6%96%AD&quot; title=&quot;#类型推断&quot;&gt;#类型推断&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E6%B3%9B%E5%9E%8B&quot; title=&quot;#泛型&quot;&gt;#泛型&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;总结：&lt;br /&gt;本文系统拆解了TypeScript函数调用时类型变量推断的完整算法流程，涵盖候选收集与候选解析两大阶段，深入分析了协变/逆变候选列表、优先级机制、联合类型分发、交叉类型处理等核心规则，并提供了&lt;code&gt;NoInfer&amp;lt;T&amp;gt;&lt;/code&gt;控制推断、提取器类型解决&quot;最烦人场景&quot;等实用技巧，帮助开发者理解并调试那些&quot;看似疯狂&quot;的类型推断行为。&lt;br /&gt;&lt;br /&gt;文章要点：&lt;br /&gt;1. 推断分两步走：先收集候选（遍历源类型与目标类型配对，遇到裸类型参数就记录），再解析候选（把列表压缩成单一类型）&lt;br /&gt;2. 候选分协变和逆变两拨：协变候选（输出位置）找公共超类型，逆变候选（输入位置）找公共子类型，通常逆变结果优先&lt;br /&gt;3. 联合类型会分发推断：源类型是联合类型时，会对每个分支独立推断，所有候选进同一个列表&lt;br /&gt;4. 交叉类型有&quot;剥离&quot;玄学：比如&lt;code&gt;A &amp;amp; {x: number}&lt;/code&gt;可能让A捕获整个对象，而&lt;code&gt;A &amp;amp; &quot;a&quot;&lt;/code&gt;可能只剥离出品牌属性，这是TS的启发式策略&lt;br /&gt;5. 条件类型的条件侧不参与推断：&lt;code&gt;T extends Foo&lt;/code&gt;不会把Foo作为T的候选，infer引入的新变量才会被收集&lt;br /&gt;6. 优先级机制会&quot;覆盖&quot;低优先级：ReturnType、LiteralKeyof等高优先级候选到来时，会清空低优先级列表&lt;br /&gt;7. 最烦人场景有解：当类型参数只出现在extends约束中而不在参数类型里时，可用提取器类型（如&lt;code&gt;GetV&amp;lt;T&amp;gt;&lt;/code&gt;）或交叉参数类型（&lt;code&gt;T &amp;amp; Map&amp;lt;K,V&amp;gt;&lt;/code&gt;）来辅助推断，但两者在联合类型支持上有取舍&lt;br /&gt;&lt;br /&gt;URL：&lt;a href=&quot;https://norswap.com/typescript-type-variable-inference/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;https://norswap.com/typescript-type-variable-inference/&quot;&gt;https://norswap.com/typescript-type-variable-inference/&lt;/a&gt;</content:encoded></item><item><title>TypeScript 泛型趣味实践</title><link>https://channel.meo-ai.com/posts/560</link><guid isPermaLink="true">https://channel.meo-ai.com/posts/560</guid><pubDate>Thu, 19 Feb 2026 16:47:31 GMT</pubDate><content:encoded>&lt;span class=&quot;post-title-source&quot;&gt;TypeScript 泛型趣味实践&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;标签：&lt;a href=&quot;/search/result?q=%23TypeScript&quot; title=&quot;#TypeScript&quot;&gt;#TypeScript&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E6%B3%9B%E5%9E%8B&quot; title=&quot;#泛型&quot;&gt;#泛型&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E5%87%BD%E6%95%B0%E9%87%8D%E8%BD%BD&quot; title=&quot;#函数重载&quot;&gt;#函数重载&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23TanStack&quot; title=&quot;#TanStack&quot;&gt;#TanStack&lt;/a&gt; &lt;a href=&quot;/search/result?q=%23%E6%9D%A1%E4%BB%B6%E7%B1%BB%E5%9E%8B&quot; title=&quot;#条件类型&quot;&gt;#条件类型&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;文章深入探讨了如何利用 TypeScript &lt;mark class=&quot;highlight&quot;&gt;泛型&lt;/mark&gt;、条件类型和函数重载，为 TanStack Start 的 Server Function 构建一个完全类型安全的查询选项封装函数。通过一个真实的场景——确保带参数和不带参数的 Server Function 都能被正确推断返回类型和参数类型——展示了从基础泛型约束到复杂类型体操的完整实现路径，最终解决了可选参数与类型推断的冲突问题。&lt;br /&gt;&lt;br /&gt;文章要点：&lt;br /&gt;- 通过为 TanStack Start 的 Server Function 封装 &lt;code&gt;refetchedQueryOptions&lt;/code&gt; 工具函数，解决查询数据返回类型为 &lt;code&gt;any&lt;/code&gt; 的类型安全问题&lt;br /&gt;- 初始方案使用泛型约束 &lt;code&gt;&amp;lt;T extends (arg: { data: any }) =&amp;gt; Promise&amp;lt;any&amp;gt;&amp;gt;&lt;/code&gt; 提取参数和返回类型，但无法处理无参函数的可选参数场景&lt;br /&gt;- 利用条件类型 &lt;code&gt;infer&lt;/code&gt; 关键字创建类型工具 &lt;code&gt;ServerFnArgs&lt;/code&gt; 来提取函数参数类型，并判断函数是否有参数&lt;br /&gt;- 通过 TypeScript 函数重载（Overloading）定义两个签名：一个用于带参数的 Server Function，一个用于无参函数，从而精确控制参数的可选性&lt;br /&gt;- 展示了泛型、条件类型与函数重载结合使用的高级技巧，实现编译时严格的类型检查和完美的类型推断&lt;br /&gt;&lt;br /&gt;链接：&lt;a href=&quot;https://frontendmasters.com/blog/fun-with-typescript-generics/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;https://frontendmasters.com/blog/fun-with-typescript-generics/&quot;&gt;https://frontendmasters.com/blog/fun-with-typescript-generics/&lt;/a&gt;&lt;a class=&quot;tgme_widget_message_link_preview&quot; href=&quot;https://frontendmasters.com/blog/fun-with-typescript-generics&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot; title=&quot;Generics are an incredibly powerful feature of TypeScript. There’s endless content on TypeScript in general, and generics in particular. This post will differ a bit and cover things more deeply.&quot;&gt;
  
  &lt;div class=&quot;link_preview_site_name accent_color&quot;&gt;Frontend Masters&lt;/div&gt;
  &lt;img class=&quot;link_preview_image&quot; alt=&quot;Fun with TypeScript Generics&quot; src=&quot;/static/https://cdn4.telesco.pe/file/XjKsqASVIPZubaS1SRvhWI-aVKLZ1mrqRpsKq2Bca4mqyvd3fKXLprre6_AAHD8S49QxR3G7g5BMm29-PlLOpTWo2nafclL0xMrKOjGOAOteQdWWcGB4LCZ_cwQUHf_Hn4bFaC5iOtnK8n7N9QT1Q1nxEGHdZ_OsYf5sJGn8dvF36MakPu2AUAJM--na1AElfo3fLwD5POgFzT009gC6Ua0rMP6_Aqe7VSPQ2HwPWg66rlZWRq-LTsVhbvLAY0NIDcgp2PQUPMP5dLRYR4VS0_7EAZPbkE0ohMNBRkbXOAQDkeuQ5pMdqseQkQVSI2TKp8DIIjmveza-6p3k4EsTjA.jpg&quot; width=&quot;1200&quot; height=&quot;630&quot; loading=&quot;eager&quot; /&gt;
  &lt;div class=&quot;link_preview_title&quot;&gt;Fun with TypeScript Generics&lt;/div&gt;
  &lt;div class=&quot;link_preview_description&quot;&gt;Generics are an incredibly powerful feature of TypeScript. There’s endless content on TypeScript in general, and generics in particular. This post will differ a bit and cover things more deeply.&lt;/div&gt;
&lt;/a&gt;</content:encoded></item></channel></rss>