I need help with adding types to a reducer function with optional dispatch. Here's the source of the pattern. https://twitter.com/FernandoTheRojo/status/1521312171262681090?s=20&t=oerzPqJ8cb5Ts3sHVMH_5Q
Here's the code:
[on, toggle] = useReducer(
(prevState, next) => next == null ? !prevState : next,
false
)
And here is my attempt:
const [on, toggle] = useReducer(
(prevState: boolean, next?: boolean) => (next == null ? !prevState : next),
false
)
const enable = () => toggle(true) // Error Expected 0 arguments, but got 1.ts(2554)
const disable = () => toggle(false) // Error Expected 0 arguments, but got 1.ts
const toggleSwitch = () => toggle() // ☑️
And here's the codesandbox with a very similar example. https://codesandbox.io/s/usereducer-patterns-6gvrgh?file=/src/reducerHooks/ToggleOrSet.tsx
Thank you.