Home > OS >  Correctly type an abstract sort by property function in typescript
Correctly type an abstract sort by property function in typescript

Time:08-13

I want to make a sort by object property function. So I have simply make it like this :

sortByProperty(object:any[],properties:string):any[]{
    return object.sort((a,b)=>{
      if(a[properties].toLowerCase()<b[properties].toLowerCase()){
        return -1;
      }
      if(a[properties]!.toLowerCase()>b[properties].toLowerCase()){
        return 1;
      }
      return 0;
    })
  }

But my problem here is that sort function lost original type and don't prevent if the property is not in the object. So, I would like to generalize it to precise the type and check if the property string is a field of the object.

Therefore, to achieve that, I have tried that :

sortByProperty<T>(object:T[],properties:string extends T):T[]{
    return object.sort((a,b)=>{
      if(a[properties].toLowerCase()<b[properties].toLowerCase()){
        return -1;
      }
      if(a[properties]!.toLowerCase()>b[properties].toLowerCase()){
        return 1;
      }
      return 0;
    })
  }

But TypeScript doesn't know that string properties is a field of T. I think string extends T is not the way to do that.

CodePudding user response:

What you are looking for is keyof type operator. You will have to make sure that the generic T is an object with string keys.

function sortByProperty<T extends Record<string, unknown>>(object: T[],properties: keyof T): T[] {
    return object.sort((a, b) => {
        if ((a[properties] as string).toLowerCase() < (b[properties] as string).toLowerCase()) {
            return -1;
        }
        if ((a[properties] as string)!.toLowerCase() > (b[properties] as string).toLowerCase()) {
            return 1;
        }
        return 0;
    });
}

CodePudding user response:

Is it maybe properties: keyof T you want?

class MyClass<T> {
   sortByProperty(object:any[],properties:keyof T):T[]{
    return object.sort((a,b)=>{
      if(a[properties].toLowerCase()<b[properties].toLowerCase()){
        return -1;
      }
      if(a[properties]!.toLowerCase()>b[properties].toLowerCase()){
        return 1;
      }
      return 0;
    })
  }
}
  • Related