Typescript中extends做条件类型时的几种行为

Extends用于条件类型判断时,有如下行为:

1,简单情况

typescript 复制代码
type A = ('X' | 'Y') extends 'X' ? '1' : '2'

作用:判断 前一个类型 能不能赋值给后一个类型

白话:

typescript 复制代码
const a: 'X' | 'Y' = p1
const b: 'X' = p2

所有能填到这个p1的位置的东西,是不是都能填到p2的位置上

2,含有泛型的情况

typescript 复制代码
type Diff<T> = T extends 'X' ? '1' : '2'

type A = Diff<'X' | 'Y'>

对于属于"裸类型参数"(即T)的检查类型,条件类型会在实例化时期自动分发到联合类型上

原文 : Conditional types in which the checked type is a naked type parameterare called distributive conditional types. Distributive conditional types are automatically distributed over union types during instantiation

白话:没有被额外包装的 泛型参数,拿来做条件类型判断时,会将该 泛型 的联合类型拆开,分别进行判断。

过程:

typescript 复制代码
'X' extends 'X' // => '1'
'Y' extends 'X' // => '2'
// 最终拿到联合类型 '1' | '2'

思考:Exclude是如何实现的?

Exclude源码:

typescript 复制代码
/**
 * Exclude from T those types that are assignable to U
 */
type Exclude<T, U> = T extends U ? never : T;

解析: T是一个泛型,它符合"裸类型参数",此时使用extends做条件判断时,传入T的联合类型会被拆开对比。

例如:

typescript 复制代码
type B = Exclude<'X'|'Y', 'X'> // type B = "Y"

实际对比的过程为:

typescript 复制代码
type Exclude<T, U> = T extends U ? never : T;
'X' extends 'X' // => never
'Y' extends 'X' // => 'Y'
// 最终得到 never | 'Y' , 也就是 'Y'

这就是Exclude的原理。

3,含有泛型,但不是"裸类型参数"

简单处理:使用元组包裹,使泛型不是一个裸类型参数

typescript 复制代码
type Diff<T> = [T] extends ['X'] ? '1' : '2'
type A = Diff<'X' | 'Y'>
// 此时不会被分发,结果:2

以下两种特殊情况不需要记忆,用到的时候查一下就好:

特殊情况 any:

当用any做检查类型,不管是否传入泛型

1:在判断条件非any的情况,都会返回判断结果的联合类型

typescript 复制代码
type Tmp1 = any extends string ? 1 : 2;  // 1 | 2
typescript 复制代码
type Tmp2<T> = T extends string ? 1 : 2;

type Tmp2Res = Tmp2<any>; // 1 | 2

2:判断条件为any的情况,仍然会进行判断

typescript 复制代码
type Special1 = any extends any ? 1 : 2; // 1
typescript 复制代码
type Special2<T> = T extends any ? 1 : 2;

type Special2Res = Special2<any>; // 1

特殊情况never

1:直接使用,仍然会进行判断

typescript 复制代码
type Tmp3 = never extends string ? 1 : 2; // 1

2:通过泛型参数传入,会跳过判断,直接返回never

typescript 复制代码
type Tmp4<T> = T extends string ? 1 : 2;

type Tmp4Res = Tmp4<never>; // never

3:判断条件为never的情况,仍然进行判断

typescript 复制代码
type Special3 = never extends never ? 1 : 2; // 1

但即使判断条件为never,如果传入泛型,也会跳过判断,直接返回never

typescript 复制代码
type Special4<T> = T extends never ? 1 : 2;

type Special4Res = Special4<never>; // never
相关推荐
笃行35023 分钟前
使用Rokid AIUI做了一个童年推箱子游戏
前端
excel1 小时前
前端加密的作用与使用场景
前端
计算机魔术师2 小时前
纽约时报版权诉讼披露:微软高管内部称训练 AI 是人类历史上最大规模劳动窃取
前端
去伪存真3 小时前
大模型的参数量为什么那么大?
前端·人工智能
IT_陈寒3 小时前
Java空指针这次真把我坑惨了
前端·人工智能·后端
掰头战士4 小时前
多重影分身!恨不得把一个agent掰成两个? 还真能干!
typescript·llm·agent
八荒启·交互动画4 小时前
# Web特效020—让 Web 特效真正动起来:时间循环应该怎么接
前端·webgl·网页特效·八荒启-交互动画·八荒启
动恰客流统计4 小时前
线下零售数字化浪潮下,客流统计的3个核心发展趋势
大数据·前端·人工智能
用户938515635075 小时前
从小米前端面试题,彻底搞懂闭包、作用域链与 useState 惰性初始化
前端·面试
粥里有勺糖6 小时前
视野修炼第133期 | Native 回春了?
前端·github·agent