D
D
Dolerum2020-01-12 17:01:23
typescript
Dolerum, 2020-01-12 17:01:23

Should you use TypeScript in Redux?

Hello.
There is a small application. I do (that is, I can say I have already done) for myself.
I recently introduced a typescript into it, and I would like to know right away whether it makes sense to use typescript in reducers (wouldn’t it work out that I’m doing this for some stupid exercise that is not quoted anywhere?) and if so, how to type it. Any guides and best practices?
Thanks for answers.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
camelCaseVlad, 2020-01-12
@camelCaseVlad

I use Flow in a project.
Phot what an example of App reducer looks like.

//@flow
import type {
  ActionWithPayloadType,
  AppStateType
} from '@project/types';
import { OPEN_MODAL, CLOSE_MODAL, SET_USER_LOGIN } from '../constants';

const initialState: AppStateType = {
  isModalOpen: false,
  isUserLoggedIn: false
};

//eslint-disable-next-line complexity
const appReducer = (
  state: AppStateType = initialState,
  action: ActionWithPayloadType = { type: '', payload: '' }
) : AppStateType => {

  if (!action) { return state; }

  if (action.type === OPEN_MODAL) {
    return { ...state, isModalOpen: true };
  }

  if (action.type === CLOSE_MODAL) {
    return { ...state, isModalOpen: false };
  }

  if (action.type === SET_USER_LOGIN) {
    return { ...state, isUserLoggedIn: action.payload };
  }

  return state;
};

export default appReducer;

@project/types
export type ActionWithPayloadType = {
  type: string,
  payload: *
};
export type AppStateType = {
  isModalOpen: boolean,
  isUserLoggedIn: boolean
}

R
Robur, 2020-01-13
@Robur

If you are translating an application into typescript, then it makes sense to translate it completely. It doesn't matter if there are reducers or something.
Here's what you definitely shouldn't - is to mix js and ts code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question