为什么当初始状态为空时,createSlice 会出错?

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

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;
};

我希望 state.app 在 redux 启动时成为 null,但稍后允许用户 select 应用程序。但是,我在 reducer selectApp 上收到了一个很长的 TypeScript 错误。这个错误是什么意思?

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

https://www.reddit.com/r/reduxjs/comments/guns93/nullable_state_best_practice_typescript_redux/

export type AuthState = AppInfo | null;

const initialState = null as AuthState; 

我在 reddit post 中找到了答案。似乎 null 会使打字稿将类型缩小为 null 而不是 AppInfo | null。使用as可以解决问题。