简体   繁体   English

在打字稿中对对象数组进行排序-元素隐含具有任何类型

[英]Sort array of objects in typescript - element implicty has any type

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 :我收到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.我尝试将index signature设置为order ,但没有成功。

What can it be?会是什么?

Since substracting string or boolean makes no sense, you should use keyof and an exclude :由于减去字符串或布尔值没有意义,您应该使用keyofexclude

export interface ProductsList {
   id: boolean
   nome: string
   qtde: number
   valor: number
   valorTotal: number
}

const exampleFn = (productsData: ProductsList[], order: Exclude<keyof ProductsList, 'id'| 'nome'>) => {
   productsData.sort((a, b) => {
       const c = a[order]
       return b[order] - a[order]
       })
}

edit : With the key check, you can also skip the exclude !编辑:通过密钥检查,您也可以跳过exclude

const exampleFn = (productsData: ProductsList[], order: keyof ProductsList) => {
   if (order !== 'id' && order !== 'nome') {
      productsData.sort((a, b) => {
         const c = a[order]
         return b[order] - a[order]
      })
   }
}

playground 操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript:元素隐含地具有 RegExp 的“任何”类型 - TypeScript: Element implicitly has an 'any' type for RegExp 映射打字稿参数:绑定元素“列”隐式具有“任何”类型 - Map typescript parameter: Binding element 'columns' implicitly has an 'any' type React typescript 错误:元素隐式具有“任何”类型 - React typescript error: Element implicitly has an 'any' type 对一组对象角度打字稿进行排序 - Sort an Array of Objects angular typescript TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type Typescript:为什么来自 HTMLCollection 的数组有 Element[] 类型 - Typescript: why array from HTMLCollection has Element[] type TypeScript 元素隐含一个“任何” - TypeScript Element implicitly has an 'any' 打字稿:可能导致此错误的原因是什么? “元素隐式具有&#39;任意&#39;类型,因为类型&#39;对象&#39;没有索引签名” - Typescript: what could be causing this error? “Element implicitly has an 'any' type because type 'Object' has no index signature” 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript Typescript “元素隐式具有‘any’类型,因为类型‘Rank’的表达式不能用于索引类型‘IPowerCards’” - Typescript "Element implicitly has an 'any' type because expression of type 'Rank' can't be used to index type 'IPowerCards'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM