Microsoft/TypeScript

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

Offen

#15.655 geöffnet am 08.05.2017

 (11 Kommentare) (1 Reaktion) (0 zugewiesene Personen)TypeScript (13.395 Forks)batch import
BugDomain: check: Control FlowHelp Wanted

Repository-Metriken

Stars
 (108.860 Sterne)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

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

Contributor Guide