You don't need to specify the type manually most of the time as TypeScript able to infer it from the default value.
In the rare event when TypeScript inference doesn't work for you, you can declare it explicitly.
I never need to type
This section is here just to assure you that. 😜
There are two use cases of
useRef, and how to provide typing for it depends on your use case.
Use it as a reference to DOM element.
To declare a
reffor DOM element, just add the element type to the
Use it as a placeholder for mutable value.
Because custom hooks are just functions, so you declare them just like how you declare them for typical functions in TypeScript.
Because types can be export and import just like variables, there is no "standard" way of organizing it, just like there is no "standard way of organizing" variables.
Usually I create a
type.ts file in the
src folder. Depends on your project, sometimes you may want to split it further. For example if your project split the code to modules, you may want to create a
<module>.type.ts file for that module to avoid your
type.ts file become a giant file.