尝试使Redux与Typescript配合使用时,参数“操作”应具有可分配给“对象”的类型

时间:2019-03-05 16:05:58

标签: typescript redux react-redux

我现在正在尝试在我的Typescript应用程序中使redux正常工作,它实际上似乎可以工作,但是typescript似乎有一些打字问题。

这就是发生的事情

enter image description here

这是我的减速器代码:

import { PopUpNotification } from "definitions/PopUpNotificationDefinitions";

interface IPopUpNotificationReducerState {
    popUpNotifications: Array<PopUpNotification>;
}

const initialState: IPopUpNotificationReducerState = {
    popUpNotifications: []
}

enum PopUpNotificationActions {
    ADD = "POPUPNOTIFICATION_ADD",
    DELETE = "POPUPNOTIFICATION_DELETE"
}

export interface AddPopUpNotification {
    type: PopUpNotificationActions,
    popUpNotification: PopUpNotification,
}

export interface DeletePopUpNotification {
    type: PopUpNotificationActions,
    popUpNotification: PopUpNotification,
}

export type PopUpNotificationAction = AddPopUpNotification | DeletePopUpNotification;

export function addPopUpNotification(popUpNotification: PopUpNotification): AddPopUpNotification {
    const addPopUpNotification: DeletePopUpNotification = {
        type: PopUpNotificationActions.ADD,
        popUpNotification: popUpNotification
    };

    return addPopUpNotification;
}

export function deletePopUpNotification(popUpNotification: PopUpNotification): DeletePopUpNotification {
    const deletePopUpNotification: DeletePopUpNotification = {
        type: PopUpNotificationActions.DELETE,
        popUpNotification: popUpNotification
    };

    return deletePopUpNotification;
}

export function popUpNotificationReducer(state = initialState, action: PopUpNotificationAction): IPopUpNotificationReducerState {

    switch (action.type) {
        case PopUpNotificationActions.ADD:
        return {
            popUpNotifications: [...state.popUpNotifications, action.popUpNotification]
        };
        case PopUpNotificationActions.DELETE:
        return {
            popUpNotifications: state.popUpNotifications.filter(notification => notification.Id !== action.popUpNotification.Id)
        }
    default:
        return initialState;
    }
}

在这里,我尝试着尽可能细致地适应redux页面上的教程。

那么,关于我现在哪里出问题的任何提示?

1 个答案:

答案 0 :(得分:0)

您可能会看到Visual Studio 2017 Community Edition附带的简单应用程序的工作示例。示例基于TypeScript,Redux和React。因此,您将获得动作,减速器等示例。

要部署示例,请使用Visual Studio 2017社区->新建项目-> Visual C#-> .NET Core-> ASP.NET Core Web App->在上方菜单中选择.NET Core 2.0(这很重要,因为.NET Core 2.1+模板基于JavaScript)-> React.js和Redux->您将拥有带有React和Redux的完全正常工作的打字稿应用程序