Microsoft/TypeScript

Strange boolean-discriminant narrowing with strictNullChecks off

已关闭

#10,564 创建于 2016年8月26日

 (7 条评论) (10 个反应) (0 位负责人)TypeScript (13,395 个派生)batch import
BugDomain: check: Control FlowEffort: ModerateHelp Wanted

仓库指标

星标
 (108,860 个星标)
PR 合并指标
 (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.

贡献者指南