Consider the following interface:
interface Theme {
color: {
primary: {
light: string
base: string
dark: string
}
secondary: {
lighter: string
light: string
base: string
dark: string
darker: string
}
}
}
I'm trying to write a type that will allow a tuple, the first element mapped to any key in colors, and the second mapped to any key under that (ie: base).
ie:
['primary', 'light'] ✅ valid
['secondary', 'darker'] ✅ valid
['primary', 'darker'] 🛑 invalid
Here is an attempt i've made on tsplayground the problem i'm facing here is that if i want to allow multiple keys to be passed as the first arg, then second needs to satisfy all of the firsts. Is there a way to tell typescript to use the literal value passed as the type?
type PickThemeColor<C extends keyof Theme['color'] = keyof Theme['color']> = [
C,
keyof Theme['color'][C]
]
// 👇🏼 this complains because 'darker' doesnt appear in both 'primary' and 'secondary' keys
const x: PickThemeColor<'primary' | 'secondary'> = ['secondary', 'darker']