Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong recognized This expression is not callable #35423

Closed
MichalKalita opened this issue Nov 30, 2019 · 3 comments
Closed

Wrong recognized This expression is not callable #35423

MichalKalita opened this issue Nov 30, 2019 · 3 comments

Comments

@MichalKalita
Copy link

MichalKalita commented Nov 30, 2019

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:

@AnyhowStep
Copy link
Contributor

return [blah, blah] as const;

@MichalKalita
Copy link
Author

Thanks @AnyhowStep
I badly read the error message.

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

philipp985 added a commit to philipp985/usehooks that referenced this issue Nov 19, 2020
Based on this thread:
microsoft/TypeScript#35423
I had to change the return type to be const to satisfy the typescript compiler
@rawjson
Copy link

rawjson commented Jul 15, 2022

Thanks, this helped. I was using a custom hook for enabling dark mode and got this error. Fixed it by returning the hook state as const.

return [darkMode, setDarkMode] as const;

benadam11 pushed a commit to uidotdev/usehooks that referenced this issue May 29, 2023
Based on this thread:
microsoft/TypeScript#35423
I had to change the return type to be const to satisfy the typescript compiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants