Currently, I am facing a challenge where I need to sort an array of objects within a function. The catch is that the function receives the key as a parameter, making it unknown:
export interface ProductsList {
id: boolean
nome: string
qtde: number
valor: number
valorTotal: number
}
const exampleFn = (productsData: ProductsList[], order: string) => {
if (order !== 'id' && order !== 'nome') {
productsData.sort((a, b) => b[order] - a[order])
}
}
The error message related to order
reads as follows:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ProductsList'.
No index signature with a parameter of type 'string' was found on type 'ProductsList'
I have attempted adding an index signature
to order
, but unfortunately, it did not solve the issue.
Any insights or suggestions on what might be causing this problem?