【发布时间】:2018-10-12 01:07:42
【问题描述】:
我有一个可区分的联合类型,它根据字符串文字字段区分类型。我想派生一个映射类型,将联合中的所有类型映射到它们对应的鉴别器文字值。
例如
export type Fetch = {
type: 'fetch',
dataType: string
};
export type Fetched<T> = {
type: 'fetched',
value: T
};
// union type discriminated on 'type' property
export type Action =
| Fetch
| Fetched<Product>;
// This produces a type 'fetch' | 'fetched'
// from the type
type Actions = Action['type'];
// I want to produce a map type of the discriminator values to the types
// comprising the union type but in an automated fashion similar to how I
// derived my Actions type.
// e.g.
type WhatIWant = {
fetch: Fetch,
fetched: Fetched<Product>
}
这在 TypeScript 中可行吗?
【问题讨论】:
标签: typescript discriminated-union