Microsoft/TypeScript

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

Open

#15,655 opened on 2017年5月8日

GitHub で見る
 (11 comments) (1 reaction) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: check: Control FlowHelp Wanted

説明

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

コントリビューターガイド