There is a 3rd party library (which cannot be altered) with the following interface:
interface MyInterface {
x: {
a: 1,
b: 2,
c: 3,
},
y: {
d: 4,
e: 5,
f: 6,
},
z: {
g: 7,
h: 8,
i: 9,
},
}
I aim to define a type (Values2ndDepth
) that represents a combination of all keys from all objects in the interface. This ensures that the following rules are applied:
let key: Values2ndDepth<MyInterface>;
key = 'a'; // valid
key = 'i'; // valid
key = 'j'; // error - as it does not exist in the above.
Below are some attempts I made:
type Values2ndDepth<T extends object> = keyof T[keyof T];
type Values2ndDepth<T extends object> = T extends {[k: string]: (infer R)} ? keyof R : never;
type Values2ndDepth<T extends object> = T extends Record<string, infer R> ? keyof R : never;
type Values2ndDepth<T extends object, K extends keyof T = keyof T> = T[K] extends object ? keyof T[K] : never;