Microsoft/TypeScript

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

Open

#15,655 创建于 2017年5月8日

在 GitHub 查看
 (11 评论) (1 反应) (0 负责人)TypeScript (48,455 star) (6,726 fork)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

贡献者指南