0

I have the following:

type Original = {
    checklist: FetchStates;
    workflowFormInfo: FetchStates;
    transitions: FetchStates;
}
type FinalResult = {checklist: FetchStates} |  { workflowFormInfo: FetchStates} | { transitions: FetchStates}

How to transform the Original, into the final result such that I don't need to manually create every entry in both sides ?

Where FinalResult would be a "computed" type.

Thank you

Renan Cidale
  • 842
  • 2
  • 10
  • 23

1 Answers1

0

I found the RequireOnlyOne type in another StackOverflow answer.

With it, you can do the following:

type RequireOnlyOne<T, Keys extends keyof T = keyof T> =
  Pick<T, Exclude<keyof T, Keys>>
  & {
  [K in Keys]-?:
  Required<Pick<T, K>>
  & Partial<Record<Exclude<Keys, K>, undefined>>
}[Keys]


type Original = {
  checklist: FetchStates;
  workflowFormInfo: FetchStates;
  transitions: FetchStates;
}

type FinalResult = RequireOnlyOne<Original>

const res1: FinalResult = {} // error
const res2: FinalResult = { checklist } // ok
const res3: FinalResult = { workflowFormInfo } // ok
const res4: FinalResult = { transitions } // ok
const res5: FinalResult = { checklist, workflowFormInfo } // error

tomaj
  • 1,570
  • 1
  • 18
  • 32