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
相关推荐
饭后一颗花生米12 小时前
2026 AI加持下前端学习路线:从入门到进阶,高效突破核心竞争力
前端·人工智能·学习
五号厂房12 小时前
TypeScript 类型导入详解:import type 与 import {type}
前端
果然_13 小时前
为什么你的 PR 总是多出一堆奇怪的 commit?90% 的人都踩过这个 Git 坑
前端·git
xpyjs13 小时前
零依赖、链式调用、可变对象:重新设计 JavaScript 颜色处理体验
前端
WayneYang13 小时前
Node.js 全栈知识点详细整理(含代码示例 + 前端结合实战)
前端·node.js
土拨鼠爱coding13 小时前
Chrome插件 - DIY Theme
前端·chrome
小赵同学WoW13 小时前
CSS作用域穿透选择器
前端·css
GreenTea13 小时前
Deep Dive into Claude Code:源码泄漏引发的AI Agent架构全解析
前端·人工智能·后端
RePeaT13 小时前
【Next.js】基础知识速查
前端·react.js·next.js
yyuuuzz13 小时前
独立站搭建:从入门到避坑实战
前端·git·github