泛型中的打字稿两层联合类型推断

Typescript two layer union type infer in generic

我想用TypeScript写一门小语言,我定义了它的基本数据类型:

type BasicDataType = {
    kind: 'text'
} | {
    kind: 'number'
};

然后我定义了一个泛型来表达它的实例:

type BasicInstance<B extends BasicDataType> = B extends { kind: 'number' } ?
    number
    : B extends { kind: 'text' } ?
    string
    : never;

let a: BasicInstance<{ kind: 'number' }> = 1;
let b: BasicInstance<{ kind: 'text' }> = '';

它运行良好,但是当我尝试定义高级类型及其实例时:

type DataType = {
    kind: 'single',
    t: BasicDataType
} | {
    kind: 'array',
    t: BasicDataType,
};
type Instance<D extends DataType> = D extends { kind: 'single', t: infer B } ?
    BasicInstance<B>
    : D extends { kind: 'array', t: infer B } ?
    Array<BasicInstance<B>>
    : never;

我收到错误:

error TS2344: Type 'B' does not satisfy the constraint 'BasicDataType'. Type 'B' is not assignable to type '{ kind: "number"; }'.

TypeScript 似乎无法理解 B 必须是 BasicDataType。为什么会发生?我该如何解决?

推断的类型并不总是像您期望的那样准确。以这个类型定义为例:

type NotWorking<T extends {x: {y: number}}> =
  T extends {x: infer N} ? N['y'] : never

失败并显示 Type '"y"' cannot be used to index type 'N'.,即使由于 T extends {x: {y: number}} 约束,N['y'] 应该存在。要进行类型检查,您可以添加另一个条件 N extends {y: number},它将始终通过:

type Working<T extends {x: {y: number}}> =
  T extends {x: infer N} ? N extends {y: number} ? N['y'] : never : never

对于您的类型,您可以在外部放置一个 B extends BasicDataType 额外条件,并使用 infer K 作为类型,这样您只需要一个 extends 条件即可涵盖所有类型:

type Instance<D extends DataType> =
  D extends { kind: infer K, t: infer B }
  ? B extends BasicDataType
    ? K extends 'single' ? BasicInstance<B> 
    : K extends 'array' ? Array<BasicInstance<B>>
    : never
   : never
: never

TypeScript playground