Skip to content
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

Proposal: Support switch/if type guards with destructuring #13403

Closed
ericanderson opened this issue Jan 10, 2017 · 2 comments
Closed

Proposal: Support switch/if type guards with destructuring #13403

ericanderson opened this issue Jan 10, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ericanderson
Copy link
Contributor

ericanderson commented Jan 10, 2017

For a lot of code duplication, its often helpful to destructure objects for later use. Type guards on switch/if help a lot in preventing unnecessary casts, but could be extended to infer a deeper understanding in some cases.

Lets use a trivial example:

interface Car {
    type: "Car",
    something: number
}

interface Truck {
    type: "Truck"
    something: boolean;
}

function takesNumber(v: number) {}
function takesBoolean(v: boolean){}

function doStuff(vehicle: Car | Truck) {
    switch (vehicle.type) {
        case "Car":
            takesNumber(vehicle.something); // okay
            break;

        case "Truck":
            takesBoolean(vehicle.something); // okay
            break;
    }
}

function doStuffDestructured(vehicle: Car | Truck) {
    const {type, something} = vehicle;

    switch (type) {
        case "Car":
            takesNumber(something); // error, not assignable
            break;

        case "Truck":
            takesBoolean(something); // error, not assignable
            break;
    }
}

In doStuffDestructured, we can logically infer that because we destructured vehicle at a moment in time, that the types/values are tied together, just as we in doStuff.

At a minimum I would propose this works for const destructuring as you can make reasonable assumptions outside of function scope:

let vehicle: Car | Truck = getVehicle();
const {type, something} = vehicle;
function doStuffDestructuredOutOfScope() {
    switch (type) {
        case "Car":
            takesNumber(something); // error, not assignable
            break;

        case "Truck":
            takesBoolean(something); // error, not assignable
            break;
    }
}

It seems to follow that for destructuring with letoutside of scope means we cannot track if the values have been assigned to something else, but could technically support let destructuring within scope. Honestly, I think its too hard to reasonable about for most end users that only supporting this for const is reasonable.

Thanks
CC/ @andy-ms @ahejlsberg @mhegazy @RyanCavanaugh

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

switch here is a red-haring. the issue is that once a variable is destructured in an assignment, the compiler can not assert any relationships between the constituents..

duplicate of #10976

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 9, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants