Search code examples
typescriptlodash

Argument of type 'string' is not assignable to parameter of type 'LoDashStatic' in second params of Lodash/fp orderBy


I got this weird problem in my React project using typescript and lodash/fp orderBy function. The orderBy function description is _.orderBy(iteratees, orders, collection). The second parameter which is orders, this can either be a string or string[], has a warning in my variable declared (I have also added an image for clearer description):

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'LoDashStatic'.ts(2769)
fp.d.ts(2298, 9): The last overload is declared here.

But whenever I used a direct value or static one, the error is gone. I'll also attached a code snippet here:

  const [sortBy, setSortBy] = useState('');
  const [sortOrder, setSortOrder] = useState<string>('asc');

  const onSort = (column: string) => {
    if (sortBy === column) {
      setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc');
    } else {
      setSortBy(column);
      setSortOrder('asc');
    }
  };

  useEffect(() => {
    if (!isEmpty(sortBy) && !isEmpty(sortOrder)) {
      const sortedList = orderBy(sortBy, sortOrder, usersList);
      setUsersList(sortedList);
    }
  }, [sortBy, sortOrder]);

The sortOrder state is the one who has the error.

Thank you for help.

enter image description here


Solution

  • Just fix it:

    const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc');