带有默认值和类型推断的 TypeScript 判别联合类型

Ben*_*n M 5 generics unions discriminated-union typescript

我想创建一个 Discriminated Union Type,它不需要传递鉴别器值。

这是我当前的代码:

interface Single<T> {
  multiple?: false // this is optional, because it should be the default
  value: T
  onValueChange: (value: T) => void
}

interface Multi<T> {
  multiple: true
  value: T[]
  onValueChange: (value: T[]) => void
}

type Union<T> = Single<T> | Multi<T>
Run Code Online (Sandbox Code Playgroud)

为了测试,我使用这个:

function typeIt<T>(data: Union<T>): Union<T> {
    return data;
}

const a = typeIt({ // should be Single<string>
    value: "foo",
    onValueChange: (value) => undefined // why value is of type any?
})

const b = typeIt({ // should be Single<string>
    multiple: false,
    value: "foo",
    onValueChange: (value) => undefined
})

const c = typeIt({ // should be Multi<string>
    multiple: true,
    value: ["foo"],
    onValueChange: (value) => undefined
})
Run Code Online (Sandbox Code Playgroud)

但是我收到了一堆错误和警告...:

  1. const aonValueChange参数的类型valueany。当multiple: false显式设置(如const b)时,它会被正确推断为string

  2. const c根本不起作用。我收到此错误:"Type 'string' is notassignable to type 'string[]'"

你知道如何解决这个问题吗?

我用这段代码创建了一个TypeScript Playground

Tit*_*mir 5

我不认为编译器可以轻松推断value回调中参数的类型,因为在检查回调时仍未确定对象文字的类型。

如果您没有很多联合成员,那么按预期工作的解决方案是使用多个重载:

export interface Single<T> {
  multiple?: false // this is optional, because it should be the default
  value: T
  onValueChange: (value: T) => void
}

interface Multi<T> {
  multiple: true
  value: T[]
  onValueChange: (value: T[]) => void
}

type Union<T> = Single<T> | Multi<T>

function typeIt<T>(data: Single<T>): Single<T>
function typeIt<T>(data: Multi<T>): Multi<T>
function typeIt<T>(data: Union<T>): Union<T> {
    return data;
}

const a = typeIt({ // is Single<string>
    value: "foo",
    onValueChange: (value) => undefined // value is typed as expected
})

const b = typeIt({ // is Single<string>
    multiple: false,
    value: "foo",
    onValueChange: (value) => undefined
})

const c = typeIt({ // is be Multi<string>
    multiple: true,
    value: ["foo"],
    onValueChange: (value) => undefined
})
Run Code Online (Sandbox Code Playgroud)