Skip to content

Annotation of boolean type ignore in favor of narrowed type #31734

@drew-gross

Description

@drew-gross

TypeScript Version: 3.5.1

Search Terms:

Boolean explicit annotation ignore

Code

// Errors as expected
let bothInferred = true;
if (bothInferred == false) { }

// Doesn't error as expected 
let bothExplicit: boolean = true as boolean;
if (bothExplicit == false) { }

// Doesn't error as expected
let leftInferredRightExplicit = true as boolean;
if (leftInferredRightExplicit == false) { }

// Why error? Narrow rhs overrides explicit declaration.
let rigthInferredLeftExplicit: boolean = true;
if (rigthInferredLeftExplicit == false) { }

Expected behavior:

4th example above shouldn't error.

Actual behavior:

Error: This condition will always return 'false' since the types 'true' and 'false' have no overlap.

Playground Link:

https://www.typescriptlang.org/play/#src=%2F%2F%20Errors%20as%20expected%0D%0Alet%20bothInferred%20%3D%20true%3B%0D%0Aif%20(bothInferred%20%3D%3D%20false)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20Doesn't%20error%20as%20expected%20%0D%0Alet%20bothExplicit%3A%20boolean%20%3D%20true%20as%20boolean%3B%0D%0Aif%20(bothExplicit%20%3D%3D%20false)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20Doesn't%20error%20as%20expected%0D%0Alet%20leftInferredRightExplicit%20%3D%20true%20as%20boolean%3B%0D%0Aif%20(leftInferredRightExplicit%20%3D%3D%20false)%20%7B%20%7D%0D%0A%0D%0A%2F%2F%20Why%20error%3F%0D%0Alet%20rigthInferredLeftExplicit%3A%20boolean%20%3D%20true%3B%0D%0Aif%20(rigthInferredLeftExplicit%20%3D%3D%20false)%20%7B%20%7D%0D%0A

Related Issues:

Tried similar patterns with other types, could not find an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions