use-reducer-async icon indicating copy to clipboard operation
use-reducer-async copied to clipboard

Typing action variable in typescript

Open DirkWolthuis opened this issue 5 years ago • 1 comments

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) {

        }
    },
};

DirkWolthuis avatar May 09 '20 16:05 DirkWolthuis

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.

dai-shi avatar May 09 '20 22:05 dai-shi