I need to define a type for an object called root
, which holds a nested array of objects called values
. Each object in the array has properties named one
(of any type) and all
(an array of the same type as one
).
Below is my attempt at creating this type declaration. However, I am unsure how to specify the type for the one
property, so currently I have used just Value<any>
:
type Value<T> = { one: T, all: T[] }
type Root = { values: Value<any>[] }
const root: Root = {
values: [
{ one: 1, all: 5 }, // This should throw an error since 'all' should be an array of numbers.
{ one: true, all: [5] }, // This should also throw an error as 'all' should be an array of booleans.
{ one: 'a', all: ['a', 'b', 'c'] }, // Okay.
{ one: true, all: [false, true, true] } // Okay.
]
}
Is there a way to achieve this without explicitly listing all possible combinations of types like in this example? It would be great if it could work for any type without specifying them individually.