use-reducer-async
use-reducer-async copied to clipboard
Typing action variable in typescript
Hi,
Is there a way to type the action variable with my type AuthenticationAsycnActions? The code bellow produces an TS error: Type 'string' is not assignable to type '"LOGOUT"'.ts(2345).
export type AuthenticationAsycnActions =
| {
type: "LOGOUT"
}
const asyncActionHandlers = {
LOGOUT: ({ dispatch }: { dispatch: React.Dispatch<AuthenticationActions> }) => async (action: AuthenticationAsycnActions) => {
try {
await FirebaseService.auth().signOut();
dispatch({ type: 'REMOVE_USER' })
}
catch (e) {
}
},
};
As far as I try something similar with examples/02_typescript/src/Person.tsx, you would either need
const asyncActionHandlers: AsyncActionHandlers<React.Reducer<State, Action>, AsyncAction> = {
or
const [state, dispatch] = useReducerAsync<React.Reducer<State, Action>, AsyncAction>(
Hope it helps. If not, I'd appreciate for a small reproduction with codesandbox.