I want to make my End-to-End testing easier and typesafer, by collecting and transforming the data-attributes in a typed constant object. However, I cannot make it work.
How can I create an union type out of an attribute value of multiple objects within an array?
interface Itest {
name: string
values: readonly string[]
}
const testAttributes: readonly Itest[] = [
{
name: "testid",
values: [
"playbarQueueIcon",
"queueBar",
"playbarPlayButton",
"playbarPauseButton",
"playbarPreviousButton",
"playbarNextButton",
// ...
] as const,
} as const,
{
name: "testgroup",
values: ["queueNextTracks", "queuePreviousTracks"] as const,
} as const,
] as const
// ***********************
// Why does this not work?
// ***********************
type ITest = typeof testAttributes[number]["values"][number]
const attr = testAttributes.reduce((acc, {name, values}) => {
for (const value of values) {
acc[value] = `[data-${name}=${value}]`
}
return acc
}, {} as Record<ITest, string>
)
attr.queueBarXXXX // => Should not work
attr.queueBar // => Should work