When dealing with a json structure from a service that contains .attributes json values, I often find myself needing to sort either by id from the DTO or by several attributes from the IContactsAttributes. The microservice returns this specific structure:
export interface IContactsDto {
_links: ILink,
attributes: IContactAttributes,
id: number,
lastUpdated: Date;
createdOn: Date;
localCreatedOn: Date;
whoUpdated: number;
location_id: number;
}
export interface IContactAttributes {
contactIsAdmin: boolean;
contactIsOperations: boolean;
contactIsMarketing: boolean;
contactLastName: string;
contactFirstName: string;
contactTitle: string;
contactEmail: string;
contactPhone: string;
}
It all works fine until I move this function to a library with strong type checking enabled and encounter the following error:
Error: src/app/microServices/dictionariesService.ts:102:32 - error TS2345: Argument of type '0 | ((a: any, b: any) => 0 | 1 | -1)' is not assignable to parameter of type '(a: any, b: any) => number'. Type 'number' is not assignable to type '(a: any, b: any) => number'.
102 return super.getAll().sort(this.sortService.sortCompareAscending('id', false, IJsonTypes.isNumber)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getAllSortedByInterfaceName() {
return super.getAll().sort(this.sortService.sortCompareAscending('contactsLastName', true, IJsonTypes.isString));
}
You can see the compare function below:
sortCompareAscending(prop, isAttribute, jsonType: IJsonTypes) {
// Code for sorting
}
Intellij suggests it should look like this:
return function(a: { [x: string]: { toLowerCase: () => number; }; }, b: { [x: string]: { toLowerCase: () => number; }; }) {
I've tried implementing that suggestion but the error persists. Can anyone provide guidance on how to define sortCompare with strong type checking enabled? Thank you for your assistance.