Argument of type 'something' is not assignable to parameter of type 'something'

Viewed 32
export enum MyNumber {
    ONE='ONE',
    TWO='TWO',
    THREE='THREE',
    FOUR='FOUR',
    FIVE='FIVE',
}

const sortByEnumOrdinal = <T>(enumType: T, enumList: T[]): T[] => {
    const orderedEnumValues = Object.values(enumType);
    const sortedEnumList = enumList.sort((a, b) => orderedEnumValues.indexOf(a) - orderedEnumValues.indexOf(b));

    return sortedEnumList;
}

const list = [MyNumber.THREE, MyNumber.FOUR, MyNumber.ONE];
sortByEnumOrdinal(MyNumber, list);

console.log(list);

The above is a enum and a generic function to sort any kind of enum list by ordinal. The function works fine, but the typescript is complaining on this line sortByEnumOrdinal(MyNumber, list);, with the error:

Argument of type 'MyNumber[]' is not assignable to parameter of type '(typeof MyNumber)[]'

What did I do wrong and how to fix this typescript error?

1 Answers

If you want to fix in your current code

change this

const list = [MyNumber.THREE, MyNumber.FOUR, MyNumber.ONE];

to this

const list = [{ONE : 'ONE',FOUR : 'FOUR', THREE : 'THREE'}]

It will fix this Error

enter image description here

Related