zimex
zimex

Reputation: 873

TypeScript: remap union type with values from object type

Is it possible to remap typescript union type with values form object type?

e.g. what I have in mid:

type Union = 'item-type' | 'category';
type Special = {
    'item-type': 'itemType';
};

type RemapedValue = [... some realy awesome typescript ...] // => 'itemType' | 'category'

Upvotes: 3

Views: 300

Answers (1)

I have tried to write as clear as possible. It is like a hack, but does the job I think.

type Union = 'item-type' | 'category' | "test2" | "test-3";
type Special = {
    'item-type': 'itemType';
    'test-3': "test";
};

type UnionExtra = Special & {
    [P in Union]: P
};

type Outersect = {
    [P in keyof UnionExtra]: UnionExtra[P]
}[Union]

type Innersect = {
    [P in (Union & keyof Special)]: Special[P]
}[keyof Special]

type Result = Outersect | Innersect;

Upvotes: 3

Related Questions