Skip to content

null checked variables in getter's closure #18243

Closed
@vmit

Description

@vmit

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

export function foo(z: string | null) {
    const zConst = z;
    if (zConst !== null) {
        return {
            get bar() {
                return zConst.length;
            }
        }
    }
}

Expected behavior:
The code is compiled with "strictNullChecks=true" option as zConst is null-checked and could not be changed.
Actual behavior:
TS is complaining about zConst being possibly null.

The problem seems is relevant to getters only since with the same zConst in a simple function scope it is fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions