20 lines
465 B
TypeScript
20 lines
465 B
TypeScript
//////////////////////
|
|
type OriginalType = {
|
|
key1: { sub1: string; sub2: string };
|
|
key2: { subA: string; subB: string };
|
|
};
|
|
|
|
type ValidKeys = keyof OriginalType;
|
|
type ValidSubKeys<K extends ValidKeys> = keyof OriginalType[K];
|
|
|
|
type EnforcedRecord = {
|
|
[K in ValidKeys]: ValidSubKeys<K>[];
|
|
};
|
|
|
|
// Example usage:
|
|
const example: EnforcedRecord = {
|
|
key1: ['sub1', 'sub2'],
|
|
key2: ['subA', 'subB', "sub1"], // last is invalid
|
|
};
|
|
|