Here's a solution (Playground link):
const PacerThemes = ['Default', 'Dark Muted', 'Neon'] as const
type ThemePropertiesVersion = {
[k in (typeof PacerThemes)[keyof typeof PacerThemes & number]]: "YourType"
}
If you wand to make this more generic by introducing some reusable helper types, you might encounter the problem that readonly any[] does not extend any[].
For example, this would not work, since typeof PacerThemes is readonly:
type Items<A extends any[]> = A[keyof A & number]
To handle this you could to something like this (Playground link):
const PacerThemes = ['Default', 'Dark Muted', 'Neon'] as const
// Version 1 -- allow only readonly arrays
type ReadonlyItems<P extends readonly any[]> = P[keyof P & number]
type TestVersion1 = NonRestrictiveItems<typeof PacerThemes>
type ThemePropertiesVersion1 = {
[k in ReadonlyItems<typeof PacerThemes>]: "YourType"
}
// Version 2 -- allow both
type NonRestrictiveItems<P extends (readonly any[]) | any[]> = P[keyof P & number]
type TestVersion2 = NonRestrictiveItems<typeof PacerThemes>
type ThemePropertiesVersion2 = {
[k in NonRestrictiveItems<typeof PacerThemes>]: "YourType"
}
type Themes = { [ThemeName in PacerTheme]: ThemeProperties; };typescriptlang.org/docs/handbook/…typeof PacerNames[number]also seems to work. I'm really not sure which thing you reference as not working.PacerTheme_s_(usingPacerThemesas an index is what's not working). [I deleted my comment so that there's no misinformation floating around.] What you suggested is perfectly fine.