Suppose I have the following and I want to create an object that has the same keys but where its value must be a transformed version of the value of the key in the original type.
export type CCDTypes = {
AuthorisationCaseEvent: AuthorisationCaseEvent,
AuthorisationCaseField: AuthorisationCaseField,
CaseEvent: CaseEvent,
CaseEventToFields: CaseEventToField,
CaseField: CaseField,
EventToComplexTypes: EventToComplexType,
Scrubbed: Scrubbed
}
at the moment I can achieve that manually with
export type COMPOUND_KEYS = {
AuthorisationCaseEvent: (keyof (AuthorisationCaseEvent))[],
AuthorisationCaseField: (keyof (AuthorisationCaseField))[],
CaseEvent: (keyof (CaseEvent))[],
CaseEventToFields: (keyof (CaseEventToField))[],
CaseField: (keyof (CaseField))[],
EventToComplexTypes: (keyof (EventToComplexType))[],
Scrubbed: (keyof (Scrubbed))[],
}
but I want COMPOUND_KEYS to be tied to CCDTypes for the type safety. When more CCDTypes are added in the future I would like typescript to complain if the data has not been added to COMPOUND_KEYS.
I intend to do a couple of other transformations like this, hence my want to create this base type CCDType type
I've tried something along the lines of the following:
Record<keyof(CCDTypes), (keyof(keyof(CCDTypes)))[]>
but the second type here ends up wanting keys that exist on an array (which makes sense since keyof(CCDTypes)
returns an array.
How can I declare this?
CodePudding user response:
You need a custom mapped type:
type AllObjectKeys<T> = {
[P in keyof T]: Array<keyof T[P]>
}