typescript - Typescript Redux - 使用 CreateSlice 输入 initialState
问题描述
我使用 CreateSlice 来创建我的减速器/动作,如下面的代码所示:
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Modal } from 'antd';
export interface ModalState {
modalProps: React.ComponentProps<typeof Modal>;
visible: boolean;
modalContent: string;
}
export interface ModalInfos {
modalProps: React.ComponentProps<typeof Modal>;
modalContent: string;
}
const initialState = {
modalProps: {},
visible: false,
modalContent: '',
};
const modal = createSlice({
name: 'modal',
initialState,
reducers: {
openModal(state, action: PayloadAction<ModalInfos>) {
state.modalProps = action.payload.modalProps;
state.modalContent = action.payload.modalContent;
state.visible = true;
},
closeModal(state) {
state.visible = false;
},
},
});
export const { openModal, closeModal } = modal.actions;
export default modal.reducer;
我没有输入我的初始状态(所以我没有使用我的 ModalState 接口),但是当我尝试这样做时:
const initialState: ModalState = {
modalProps: {},
visible: false,
modalContent: '',
};
然后我在以下行收到一条奇怪的错误消息:
state.modalProps = action.payload.modalProps;
消息是:
Type 'PropsWithChildren<ModalProps>' is not assignable to type 'WritableDraft<PropsWithChildren<ModalProps>>'.
Types of property 'okButtonProps' are incompatible.
Type 'Partial<{ href: string; target?: string | undefined; onClick?: ((event: MouseEvent<HTMLElement, MouseEvent>) => void) | undefined; } & BaseButtonProps & Pick<...> & { ...; } & Pick<...>> | undefined' is not assignable to type 'WritableDraft<Partial<{ href: string; target?: string | undefined; onClick?: ((event: MouseEvent<HTMLElement, MouseEvent>) => void) | undefined; } & BaseButtonProps & Pick<...> & { ...; } & Pick<...>>> | undefined'.
Type 'Partial<{ href: string; target?: string | undefined; onClick?: ((event: MouseEvent<HTMLElement, MouseEvent>) => void) | undefined; } & BaseButtonProps & Pick<...> & { ...; } & Pick<...>>' is not assignable to type 'WritableDraft<Partial<{ href: string; target?: string | undefined; onClick?: ((event: MouseEvent<HTMLElement, MouseEvent>) => void) | undefined; } & BaseButtonProps & Pick<...> & { ...; } & Pick<...>>>'.
Types of property 'defaultValue' are incompatible.
Type 'string | number | readonly string[] | undefined' is not assignable to type 'string | number | string[] | undefined'.
Type 'readonly string[]' is not assignable to type 'string | number | string[] | undefined'.
The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'.ts(2322)
可以不为我的 initalState 分配类型吗?如果不是,“state.modalProps”和“action.payload.modalProps”的类型在我看来是相同的,所以我不知道如何解决这个问题......
解决方案
不,您必须为您的initialState
. 这就是解释其他减速器createSlice
的实际类型的方式。state
你需要:
const initialState : ModalState = {
modalProps: {},
visible: false,
modalContent: '',
};
在这里定义两个不同的接口并没有错。也就是说,如果您真的想避免必须两次定义两个公共字段,您可以这样做:
export interface ModalInfos {
modalProps: React.ComponentProps<typeof Modal>;
modalContent: string;
}
export interface ModalState extends ModalInfos {
visible: boolean;
}
或者:
export interface ModalState {
modalProps: React.ComponentProps<typeof Modal>;
visible: boolean;
modalContent: string;
}
export type ModalInfos = Omit<ModalState, 'visible'>;
推荐阅读
- javascript - 5天预测在改变城市时不断增加天数
- python - 阻止 selenium/itertools 跳过从 Excel 电子表格输入的行
- matlab - 如何使用 plot3 绘制 3D 矩阵
- django - 如何修复在 Django Rest Framework 中使用 REST API 登录时出现的 CSRF 错误?
- mysql - 线程中未处理的异常由
.wrapper 在 0x10e2d62f0> - ios - iOS WidgetKit:如何显示相册中的图片
- javascript - 如何在 Reactjs 中调用组件?
- swift - Xcode 文件未在 Git Pull 上更新
- python - 迭代列表时,“方法”对象不可迭代
- java - QtJambi5+ 构建过程