I'm trying to sort an array of objects within a function, however the function receives the key as a parameter, so it's 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])
}
}
I'm getting this error with order:
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've tried to set an index signature to order, but without success.
What can it be?
order? It's not an arbitrary string, but specifically those keys ofProductsListthat have a number-valued property, like this, right? If so I could write up an answer; if not, what am I missing?booleanorstringdoesn't make sense.