My goal is to manipulate data structured like this:
const exampleState = {
elements : {
element1: {
values: {
value1: 10,
value2: 10,
},
elementDetails : {
detail1 : {
values: {
value1: 10,
value2: 10,
},
informationDisplayed: false
}
}
}
}
}
where the columns can be dynamic parameters.
So, I defined the following types and interfaces:
type State<T extends GenericColumnList> = T & {
elements: elementsSet<T>
}
type GenericColumnList = Record<string, number>
type elementsSet<T extends GenericColumnList> = Partial<Record<string, ElementRow<T>>>
export interface ElementRow<T extends GenericColumnList> {
elementDetails: DetailElementsSet<T>
values: T
}
type DetailElementsSet<T> = Partial<Record<string, ElementDetailRow<T>>>
export interface ElementDetailRow<T> {
values: T
informationDisplayed: boolean
}
However, when trying to pass an interface called 'ColumnList' as a parameter to the 'State' type like this:
export interface MyColumns {
column1?: number
column2?: number
}
this declaration causes an error in TypeScript. For example:
const exampleState: State<MyColumns> = {
elements : {
element1: {
values: {
value1: 10,
value2: 10,
},
elementDetails : {
detail1 : {
values: {
value1: 10,
value2: 10,
},
informationDisplayed: false
}
}
}
}
}
The TypeScript compiler raises an error stating 'The type MyColumns does not comply with Record<string, number>' but I am unsure why this occurs.