Skip to content

Breaking change: Use of 'void' in control flow constructs is now disallowed #26262

@RyanCavanaugh

Description

@RyanCavanaugh

TypeScript Version: 3.1

Search Terms: boolean void truthy falsy if while do for not

An expression of type 'void' cannot be tested for truthiness

Code

Consider this code:

function existsAndIsCool(fileName: string) {
  fs.exists(fileName, exists => {
    return exists && isCool(fileName);
  });
}

if (existsAndIsCool("myFile")) {
  console.log("Cool!");
} else {
  console.log("Not cool :(");
}

Expected behavior: The "Cool!" branch is actually unreachable because existsAndIsCool returns void, not boolean. I should have been warned about this at some point.

Actual behavior: No error, never cool 😢

Playground Link: Link

Related Issues:
PR #26234
#7256
#10942
#7306

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions