Skip to content

Unintuitive compiled output when using array destructuring to swap variables #7297

Closed
@joshuacc

Description

@joshuacc

TypeScript Version:

1.8.2

Code

function whatever (idA, idB) {
    [idA, idB] = [idB, idA];

    return true; 
}

Expected behavior:

function whatever(idA, idB) {
    var _a;
    _a = [idB, idA], idA = _a[0], idB = _a[1];
    return true;
}

Actual behavior:

function whatever(idA, idB) {
    _a = [idB, idA], idA = _a[0], idB = _a[1];
    return true;
    var _a;
}

Thanks to variable hoisting, there is no functional problem here. However, I consider the compiled code unintuitive.

In addition, using Istanbul for code coverage highlights the var _a; declaration as not covered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadRevisitAn issue worth coming back to

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions