Skip to content

Anonymous function breaks discriminated intersection type narrowing #9929

Closed
@frederikschubert

Description

@frederikschubert

TypeScript Version:
2.0.0
Code

class ControllerSelectAction implements Action {
    type: "CONTROLLER_SELECT";
    payload: Controller;
}

export class ControllerRefreshAction implements Action {
    type: "CONTROLLER_REFRESH";
}

type ControllerAction = ControllerSelectAction | ControllerRefreshAction;

const controllerReducer(state: any, action: ControllerAction) {
    switch(action.type) {
        case "CONTROLLER_REFRESH":
        return state.set("refresh", true);

        case "CONTROLLER_SELECT":
        return state.update("selected", (controller) => controller.id === action.payload.id ? controller : action.payload));
}

Expected behavior:
action should be narrowed to the ControllerSelectAction class in the anonymous function in the CONTROLLER_SELECT case, so that no error is thrown when accessing action.payload.id.

Actual behavior:
The following error is thrown:

Property 'payload' does not exist on type 'ControllerSelectAction | ControllerRefreshAction

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeDuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions