Typescript: Not all constituents of type ‘boolean | (() => void)’ are callable
The full error:
This expression is not callable. Not all constituents of type 'boolean | (() => void)' are callable. Type 'false' has no call signatures. TS2349
This is the code that was triggering the error:
export const useLoading = (defaultValue: boolean = false) => {
const [loading, setLoading ] = useState(defaultValue)
const startLoading = () => setLoading(true)
const stopLoading = () => setLoading(false)
return [
loading,
startLoading,
stopLoading
]
}
CAUSE: the return of the array. Having arrays without a specific type gave me this error as wel: Line 0: Parsing error: : Cannot read property ‘map’ of undefined. The solution was to typehint the array.
SOLUTION: don’t return the array just like that, add ‘as const
` at the end of that return statement.
export const useLoading = (defaultValue: boolean = false) => {
const [loading, setLoading ] = useState(defaultValue)
const startLoading = () => setLoading(true)
const stopLoading = () => setLoading(false)
return [
loading,
startLoading,
stopLoading
] as const
}
This is the post that showed me where to find it: https://github.com/microsoft/TypeScript/issues/35423