Search code examples
typescriptreact-typescript

type "undefined" is not assignable to type 1 | -1


I'm new to typescript and I'm trying to implement sorting to my website and I'm trying to do it using functio sortItems

    const sortItems: (a: typeSort, b: typeSort) => () => 1 | -1 = (a, b) => {
        const author1: string = a.author as string;
        const author2: string = b.author as string;
        switch (currentSort) {
            case 'AUTHOR':
                return () => {
                    if (author1 > author2) return 1;
                    if (author1 < author2) return -1;
                };
            default:
                return () => {
                    if (a.name > b.name) return 1;
                    if (a.name < b.name) return -1;
                };
        }
    };

but I all the time get an error

Type '(a: typeSort, b: typeSort) => () => 1 | -1 | undefined' is not assignable to type '(a: typeSort, b: typeSort) => () => 1 | -1'.
  Call signature return types '() => 1 | -1 | undefined' and '() => 1 | -1' are incompatible.
    Type '1 | -1 | undefined' is not assignable to type '1 | -1'.
      Type 'undefined' is not assignable to type '1 | -1'.ts(2322)

and I tried just adding undefined to my return types, but then I get an error in array.sort(sortItems)

Argument of type '(a: typeSort, b: typeSort) => () => 1 | -1' is not assignable to parameter of type '(a: { createdAt: string; _id: string; name: string; description: string; author?: string | undefined; startId?: string | undefined; items?: { _id: string; name: string; description?: string | undefined; }[] | undefined; }, b: { ...; }) => number'.
  Types of parameters 'a' and 'a' are incompatible.
    Type '{ createdAt: string; _id: string; name: string; description: string; author?: string | undefined; startId?: string | undefined; items?: { _id: string; name: string; description?: string | undefined; }[] | undefined; }' is not assignable to type 'typeSort'.
      Types of property 'author' are incompatible.
        Type 'string | undefined' is not assignable to type 'string'.
          Type 'undefined' is not assignable to type 'string'.ts(2345)

my typeSort type is:

type typeSort = {
    createdAt: string;
    _id: string;
    name: string;
    description: string;
    author?: string;
    startId?: string | undefined;
    items?: { _id: string; name: string; description?: string | undefined }[] | undefined;
};

How can I force typescript to define that return is always 1 or -1? I tried using something like

if(a === undefined || b === undefined){
    ...restOfSwitch
}

but it doesn't work :(


Solution

  • I assume your error is when you do:

    return () => {
                    if (author1 > author2) return 1;
                    if (author1 < author2) return -1;
                };
    

    What happens if author1 == author2? You don't return 1 or -1 but undefined which is wrong.

    A possible solution

    You can simplify the return statements with a ternary operator and get rid of the unnecessary complexity:

    switch (currentSort) {
            case 'AUTHOR':
                return author1 > author2 ? 1 : -1
            default:
                return a.name > b.name ? 1 : -1
        }