Skip to content

Boolean function returns null #8892

Closed
Closed
@colltoaction

Description

@colltoaction

TypeScript Version:

1.8.10

Code

// A self-contained demonstration of the problem follows...
function bool_fun(value: string) : boolean {
    return value && value == "bug";
}

console.log(bool_fun(null)); // should log false or throw a compiler error, but logs null
console.log(bool_fun("false"));
console.log(bool_fun("bug"));

A boolean value should always be true or false, but never null (even if it's a falsy value). This would break strict comparisons like ===. The spec is also very clear: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.2.2

Expected behavior:
Either we have a compiler error or the console prints:

false
false
true

Actual behavior:
The console prints:

null
false
true

Metadata

Metadata

Assignees

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions