理解 TypeScript 条件类型与类型推断

在 TypeScript 中,条件类型与类型推断为开发者提供了强大的工具,用于灵活地定义复杂的类型。以下将逐步解析代码:

typescript 复制代码
type TGetMixinMethods<T> = T extends { methods?: infer M } ? M : never;

并详细说明其含义,同时提供可运行的代码示例来帮助更好地理解。

type

在 TypeScript 中,type 关键字用于定义类型别名。类型别名是对一个复杂类型的命名,使其更具可读性和可重用性。

示例:

typescript 复制代码
type StringAlias = string;
const name: StringAlias = `John Doe`;

在上述代码中,StringAliasstring 的别名。

TGetMixinMethods<T>

TGetMixinMethods<T> 是一个类型别名,其定义依赖于泛型 T。泛型允许类型别名在声明时并不具体指定类型,而是在使用时传递具体的类型。

示例:

typescript 复制代码
type Identity<T> = T;
const value: Identity<number> = 42; // T 被推断为 number

T extends { methods?: infer M }

T extends { methods?: infer M } 是条件类型的一部分。以下逐步拆解:

  1. T extends { methods?: infer M }
    • extends:条件类型的判断部分,用于检查 T 是否符合 { methods?: infer M } 的结构。
    • { methods?: infer M }:表示 T 应该是一个包含可选属性 methods 的对象类型。methods 的值可以是任何类型,具体类型由 infer M 捕获。
    • infer M:TypeScript 的类型推断机制,表示如果 T 满足条件,则推断 methods 的类型为 M

示例:

typescript 复制代码
type Example = { methods?: () => void };
type Check = Example extends { methods?: infer M } ? M : never; // 推断 M 为 () => void

在上述代码中,Check 的类型为 () => void,因为 Example 满足条件。

? M : never

? M : never 是条件类型的分支逻辑:

  • 如果 T 符合 { methods?: infer M },则结果类型为 M
  • 如果 T 不符合条件,则结果类型为 never

示例:

typescript 复制代码
type Example1 = { methods?: () => void };
type Example2 = { name: string };
type Result1 = Example1 extends { methods?: infer M } ? M : never; // () => void
type Result2 = Example2 extends { methods?: infer M } ? M : never; // never

完整示例

为了更直观地理解 TGetMixinMethods,以下提供一个完整的例子:

typescript 复制代码
type TGetMixinMethods<T> = T extends { methods?: infer M } ? M : never;

// 示例对象类型
type Component = {
  methods?: {
    sayHello: () => void;
    add: (a: number, b: number) => number;
  };
};

type Methods = TGetMixinMethods<Component>;
// Methods 的类型为:{
//   sayHello: () => void;
//   add: (a: number, b: number) => number;
// }

// 实际使用示例
const componentMethods: Methods = {
  sayHello: () => console.log(`Hello!`),
  add: (a, b) => a + b,
};

componentMethods.sayHello();
console.log(componentMethods.add(2, 3));

在该示例中:

  1. Component 是一个对象类型,包含一个可选的 methods 属性。
  2. TGetMixinMethods<Component> 提取了 methods 的类型,并将其赋值给 Methods
  3. Methods 被用作变量 componentMethods 的类型。

应用场景

TGetMixinMethods 通常用于需要从某些对象类型中提取特定子类型的场景。例如:

  1. 组件库设计 :在一个组件库中,组件可能会通过 methods 提供特定的方法集。TGetMixinMethods 可以提取这些方法的类型,用于进一步的类型检查。
  2. 动态模块加载 :在动态模块加载场景中,TGetMixinMethods 可以用来从模块定义中提取接口。

源码运行

为了验证代码的正确性,可以将以下代码粘贴到 TypeScript 编译器中运行:

typescript 复制代码
type TGetMixinMethods<T> = T extends { methods?: infer M } ? M : never;

type Component = {
  methods?: {
    sayHello: () => void;
    add: (a: number, b: number) => number;
  };
};

type Methods = TGetMixinMethods<Component>;

const componentMethods: Methods = {
  sayHello: () => console.log(`Hello!`),
  add: (a, b) => a + b,
};

componentMethods.sayHello();
console.log(componentMethods.add(2, 3));
相关推荐
慧一居士31 分钟前
<script setup>中的setup作用以及和不带的区别对比
前端
RainbowSea1 小时前
NVM 切换 Node 版本工具的超详细安装说明
java·前端
读书点滴1 小时前
笨方法学python -练习14
java·前端·python
Mintopia1 小时前
四叉树:二维空间的 “智能分区管理员”
前端·javascript·计算机图形学
慌糖1 小时前
RabbitMQ:消息队列的轻量级王者
开发语言·javascript·ecmascript
Mintopia1 小时前
Three.js 深度冲突:当像素在 Z 轴上玩起 "挤地铁" 游戏
前端·javascript·three.js
Penk是个码农1 小时前
web前端面试-- MVC、MVP、MVVM 架构模式对比
前端·面试·mvc
MrSkye1 小时前
🔥JavaScript 入门必知:代码如何运行、变量提升与 let/const🔥
前端·javascript·面试
白瓷梅子汤2 小时前
跟着官方示例学习 @tanStack-form --- Linked Fields
前端·react.js
爱学习的茄子2 小时前
深入理解JavaScript闭包:从入门到精通的实战指南
前端·javascript·面试