Skip to content

Anonymous function breaks discriminated intersection type narrowing #9929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
frederikschubert opened this issue Jul 25, 2016 · 2 comments
Closed
Labels
Breaking Change Would introduce errors in existing code Duplicate An existing issue was already created

Comments

@frederikschubert
Copy link

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
@sandersn
Copy link
Member

This is a known breaking change in 2.0: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes. See also #7662.

@sandersn sandersn added Duplicate An existing issue was already created Breaking Change Would introduce errors in existing code labels Jul 25, 2016
@frederikschubert
Copy link
Author

I didn't know that document yet. Sorry for the duplicate and thanks for your quick answer! :)

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants