gale/type-experiments-01.ts

20 lines
465 B
TypeScript
Raw Normal View History

2024-08-02 14:45:14 -04:00
//////////////////////
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
};