Typescript: Wrong recognized `This expression is not callable`

Created on 30 Nov 2019  路  2Comments  路  Source: microsoft/TypeScript


TypeScript Version: All stable versions in playground (2.4.1 ... 3.7.2)


Search Terms:
This expression is not callable.

Code

const useLocalStorage = () => {
  const setValue = (value: string | ((oldValue: string) => string)) => { };

  // return [setValue];  // this work
  return ["", setValue]; // this doesn't work
}

// const [setValue] = useLocalStorage(); // this work
const [,setValue] = useLocalStorage(); // this doesn't work

setValue((state) => "");
/*^^^^^^
  This expression is not callable.
    Not all constituents of type 'string | ((value: string | ((oldValue: string) => string)) => void)' are callable.
      Type 'string' has no call signatures.(2349)
*/

Expected behavior:
It should works in any way of return internal function out.

Actual behavior:
This error doesn't makes sense, because if I return just setValue function will work.

Playground Link:
playground

Related Issues:

Most helpful comment

return [blah, blah] as const;

All 2 comments

return [blah, blah] as const;

Thanks @AnyhowStep
I badly read the error message.

Const assertion or make types for function result is a solution. 馃憤

Was this page helpful?
0 / 5 - 0 ratings