react-use-localstorage is a React Hook that returns an array of Local Storage item and a way to set it
I followed TheAifam5’s instruction to generate a type file and distributed it.
But when I tried to use the new distrubution within a React + TypeScript project, I was getting the following error.
Below is the full source code for
As you can see,
useLocalStorage returns an array of
When auto-generating a type file using
tsc, TypeScript generates following definition code.
TypeScript inferred the return type as
(string | ((item: string) => void)) which is not right.
So to fix it you need to explicitly declare the return type of
useLocalStorage to generate a correct type definition.
You can now see that TypeScript has generated the definition correctly.
And TypeScript is happy (in VS Code).
🤔 Question to readers
Does anyone know why TypeScript wasn’t able to infer the return type correctly?
*** UPDATE: 2019-02-12 ***
The gist is that, TypeScript does not infer tuple type variable because there isn’t enough information.