Microsoft/TypeScript

Strange boolean-discriminant narrowing with strictNullChecks off

Open

#10,564 建立於 2016年8月26日

在 GitHub 查看
 (7 留言) (10 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: check: Control FlowEffort: ModerateHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

TypeScript Version: nightly (2.1.0-dev.20160826)

Code

type Result = { success: true }
            | { success: false, error: string }

function handleError(res: Result) {
    if (res.success === true) {
        return;
    }

    res.error; // OK
}

function handleError2(res: Result) {
    if (res.success !== false) {
        return;
    }

    res.error; // OK
}

function handleError3(res: Result) {
    if (res.success) {
        return;
    }

    res.error; // Property 'error' does not exist on type 'Result'
               // but should be OK
}

Expected behavior: All three cases should behave the same.

貢獻者指南