30 lines
853 B
TypeScript
30 lines
853 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 FixedLengthArray<T, L extends number> = T[] & { length: L };
|
||
|
|
||
|
type EnforcedRecord<L extends number> = {
|
||
|
[K in ValidKeys]: FixedLengthArray<ValidSubKeys<K>, L>;
|
||
|
};
|
||
|
|
||
|
type FixedArr<Arr, Length extends number> = Arr & { length: Length };
|
||
|
function Func<L extends number>(arr1:FixedArr<string[], L>)
|
||
|
{
|
||
|
return arr1.length;
|
||
|
}
|
||
|
|
||
|
|
||
|
const test = Func<3>(["a", "b", "c"])
|
||
|
|
||
|
// Example usage:
|
||
|
const example: EnforcedRecord<2> = {
|
||
|
key1: ['sub1', 'sub2', 'sub1'], // Valid
|
||
|
key2: ['subA', 'subB'], // Valid
|
||
|
// key1: ['sub1'], // Invalid, TypeScript will give an error
|
||
|
// key2: ['subA', 'subB', 'subC'], // Invalid, TypeScript will give an error
|
||
|
};
|