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

TypeScript移除readonly后extends行为异常:Bug还是限制?

TypeScript深度精确类型校验的嵌套对象行为异常问题

问题背景

在数据库操作场景中,需要实现深度精确类型校验——既不像简单的extends检查那样允许额外属性,也不像严格的Equal类型那样要求完全匹配,作为中间方案来校验数据结构的有效性。该类型支持两种输入:通过as const创建的readonly类型,或直接定义的类型别名,用于数据库操作前的合法性校验。

核心异常表现

实现的精确类型校验在三层嵌套对象场景下出现不一致行为:

  • 使用直接定义的类型别名时,无效值会被正确推断为never;
  • 使用as const生成的类型(移除readonly修饰后)时,无效值未被推断为never,校验失效;
  • 两层嵌套对象场景下,两种方式的校验均正常工作。

版本差异与Bug状态

该行为在不同TypeScript版本中表现不一致,已针对此问题提交Bug报告。

最小复现代码

// 基础类型定义
type Equal<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? true : false;

type DeepMutable<T> = {
  -readonly [K in keyof T]: T[K] extends object ? DeepMutable<T[K]> : T[K];
};

type Exact<T, U> =
  T extends U ?
  (U extends T ? true : never) :
  never;

// 三层嵌套测试类型
type ThreeLevelAlias = {
  a: {
    b: {
      c: number;
    };
  };
};

const threeLevelConst = {
  a: {
    b: {
      c: 123;
    }
  }
} as const;

type ThreeLevelConst = DeepMutable<typeof threeLevelConst>;

// 校验结果对比
type TestAlias = Exact<ThreeLevelAlias & { extra: string }, ThreeLevelAlias>; // 结果:never(符合预期)
type TestConst = Exact<ThreeLevelConst & { extra: string }, ThreeLevelConst>; // 结果:true(不符合预期)

// 两层嵌套测试(无异常)
type TwoLevelAlias = {
  a: {
    b: number;
  };
};

const twoLevelConst = {
  a: {
    b: 123;
  }
} as const;

type TwoLevelConst = DeepMutable<typeof twoLevelConst>;

type TestTwoAlias = Exact<TwoLevelAlias & { extra: string }, TwoLevelAlias>; // 结果:never(符合预期)
type TestTwoConst = Exact<TwoLevelConst & { extra: string }, TwoLevelConst>; // 结果:never(符合预期)

可能的原因分析

如果此行为属于TypeScript的预期行为,可能与以下机制有关:

  1. 深层readonly类型的解构逻辑:as const生成的深层readonly类型在通过DeepMutable移除readonly修饰时,三层嵌套层级下类型检查器可能未完全解构嵌套对象的结构,导致后续Exact类型检查无法正确识别额外属性;
  2. 类型兼容性的层级处理:对于as const生成的字面量类型(即使移除readonly),TypeScript在处理深层嵌套对象的类型兼容性时,可能存在特殊的层级优化,导致三层嵌套时的额外属性检查被忽略;
  3. 类型推断的边界限制:在深层嵌套场景下,类型检查器对as const生成的类型进行扩展(如添加额外属性)时,可能未正确传递层级约束,导致Exact类型的双向extends检查失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 07:45:36