You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript类型交叉类型产生never类型的原因及特定交叉表达式结果的技术咨询

Understanding These Two TypeScript Intersection Type Results

Let's break down why these two intersection type expressions produce their respective results—it all boils down to how TypeScript merges conflicting properties in intersection types.

1. Why { d: true; e: number; } & { d: false; e: number; } resolves to never

An intersection type (&) requires a value to satisfy all the types it combines. Let's zero in on the d property here:

  • The first type enforces d must be true
  • The second type enforces d must be false

There’s no possible value that can be both true and false at the same time. Since the d property alone has no valid possible values, the entire combined type has zero valid values too. That’s exactly what the never type represents: a type with no possible runtime values.

Which is why in your code example, you can only assign an error-throwing expression to a variable of type DataTypeNever—there’s no legal value that fits the type:

type DataTypeNever = { d: true; e: number; } & { d: false; e: number; }
let data: DataTypeNever = (() => { throw new Error })() // Only works because this never returns a valid value

2. Why { d: false; e: number; } & { d: false; e: string; } resolves to { d: false, e: never }

Here, the d property is consistent across both types—both specify d must be false. So TypeScript merges these into a single d: false property with no conflict.

Now look at the e property:

  • The first type requires e to be a number
  • The second type requires e to be a string

Just like with the d property in the first example, there’s no value that is both a number and a string. So the e property’s type becomes never (no valid values possible).

The combined type ends up as { d: false, e: never } because:

  • d has a valid, consistent value (false)
  • e has no valid values (never)

Again, this is why you can only assign an error-throwing expression to the e property—there’s no valid number/string hybrid value to use:

type DataType = { d: false; e: number; } & { d: false; e: string; }
let data: DataType = { d: false, e: (() => { throw new Error })() }

内容的提问来源于stack exchange,提问作者Jason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 13:47:45