Skip to content

Noncrashing property access through non-null assertion operator should narrow that property to NonNullable #15655

Open
@LeviticusMB

Description

@LeviticusMB

TypeScript Version: 2.3 (Playground)

Code

Using strictNullChecks.

interface Foo {
    optional?: number;
}

interface Bar {
    foo?: Foo;
}

function test(bar: Bar) {
    if (bar.foo!.optional) {
        let num: number = bar.foo.optional;
    }

    if (bar.foo && bar.foo.optional) {
        let num: number = bar.foo.optional;
    }
}

Expected behavior:
No errors or warnings.

Actual behavior:

  • num in first if block is number | undefined
  • bar.foo in first if block can be undefined

skarmavbild 2017-05-08 kl 12 58 06

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions