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
相关推荐
鹏程十八少5 分钟前
4.Android 30分钟手写一个简单版shadow, 从零理解shadow插件化零反射插件化原理
android·前端·面试
亿元程序员12 分钟前
这款值68亿的游戏,你不实战一下吗?安排!
前端
摸鱼的春哥1 小时前
Agent教程15:认识LangChain(中),状态机思维
前端·javascript·后端
明月_清风1 小时前
告别遮挡:用 scroll-padding 实现优雅的锚点跳转
前端·javascript
明月_清风1 小时前
原生 JS 侧边栏缩放:从 DOM 监听到底层优化
前端·javascript
万少10 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站12 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名15 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫15 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊15 小时前
React 19 对比 React 16 新特性解析
前端·react.js