Skip to content

strictNullChecks non-null assertion lost inside Array#map callback #10982

Closed
@Swatinem

Description

@Swatinem

TypeScript Version: nightly (2.1.0-dev.20160919)

Code

function f(_: string) {}

const foo: {bar: null | string} = {bar: "foo"};

if (foo.bar) {
    foo.bar.length; // works fine

    (function () {
        // works fine
        foo.bar.length;
        f(foo.bar);
    })();

    // both fail non-null assertion
    [].map(_ => {
        foo.bar.length;
        f(foo.bar);
    });
}

Expected behavior:

There should be no compile errors, as foo.bar is guaranteed to be non-null inside the whole block

Actual behavior:

Inside the map callback, TS thinks that foo.bar can be null. Strangely enough, an IIFE works just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions