3

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'

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to make that object key value from type Special would not be mandatory in type Union? e.g. Playground Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.