Why is there an error on createSlice when the initial state is null?

Viewed 2417
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { AppInfo } from '../models';

const initialState: AppInfo | null = null;

const appSlice = createSlice({
  name: 'app',
  initialState,
  reducers: {
    selectApp(state, action: PayloadAction<AppInfo>) {
      return action.payload;
    },
  },
});

export const { selectApp } = appSlice.actions;
export default appSlice.reducer;

// models.ts
export type AppInfo = {
  appName: string;
  createDate: string;
  develop: string;
  op: string;
};

I want state.app to be null when redux starts, but later allow the user to select the app. However, I got a long TypeScript error on the reducer selectApp. What does this error mean?

(method) selectApp(state: null, action: PayloadAction<AppInfo>): AppInfo
Type '(state: null, action: { payload: AppInfo; type: string; }) => AppInfo' is not assignable to type 'CaseReducer<null, { payload: any; type: string; }> | CaseReducerWithPrepare<null, PayloadAction<any, string, any, any>>'.
Type '(state: null, action: { payload: AppInfo; type: string; }) => AppInfo' is not assignable to type 'CaseReducer<null, { payload: any; type: string; }>'.
Type 'AppInfo' is not assignable to type 'void'.ts(2322)
1 Answers
Related