2

I would like to create a reducer with typescript!

paymentReducer.tsx

let initialState = {
    saved: [],
};

export interface Payment {
    saved: ISavedPayment[];
}

export let payment = (
    state = initialState,
    { type, payload }: PaymentActions
): Payment => {
    switch (type) {
        case SAVED_PAYMENTS_LOADED:
            return { ...state, saved: payload };
        default:
            return state;
    }
};

No errors! But the type of saved array in state parameter of reducer becomes never. But it should be ISavedPayment[]?

(parameter) state: { saved: never[]; }

2
  • Why don't you specify the type explicitly? Commented Sep 15, 2020 at 10:51
  • Could you add any code? Commented Sep 15, 2020 at 10:52

1 Answer 1

2

Specify the type of initialState explicitly:

const initialState: { saved: ISavedPayment[] } = {
    saved: [],
};

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.