Skip to content

Variable assignment with destructuring incorrectly marks locals as read #26345

@calebsander

Description

@calebsander

TypeScript Version: 3.1.0-dev.20180809

Search Terms:
noUnusedLocals
destructuring assignment
declared but its value is never read

Code

// Compile with --noUnusedLocals

{
    const abc = 1 // 'abc' is declared but its value is never read.
    let def: number // 'def' is declared but its value is never read.
    def = 2
}
{
    const {abc} = {abc: 1} // 'abc' is declared but its value is never read.
    let def: number // no compiler error
    ({def} = {def: 2})
}
{
    const [abc] = [1] // 'abc' is declared but its value is never read.
    let def: number // no compiler error
    [def] = [2]
}

Expected behavior:
All 6 variable declarations should cause a compiler error because they are declared but never read. It should not matter whether the variable is assigned to where it is declared, or afterwards.

Actual behavior:
Destructuring assignments to variables after they are declared marks them as read, even though they are only written to. Direct assignments work as expected.

Playground Link:
N/A; playground doesn't support noUnusedLocals

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions