Microsoft/TypeScript

Strange boolean-discriminant narrowing with strictNullChecks off

Fechada

#10.564 aberto em 26 de ago. de 2016

 (7 comentários) (10 reações) (0 responsável)TypeScript (13.395 forks)batch import
BugDomain: check: Control FlowEffort: ModerateHelp Wanted

Métricas do repositório

Stars
 (108.860 estrelas)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

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.

Guia do colaborador