Skip to content

Boolean function returns null #8892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
colltoaction opened this issue May 31, 2016 · 5 comments
Closed

Boolean function returns null #8892

colltoaction opened this issue May 31, 2016 · 5 comments
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@colltoaction
Copy link

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
@basarat
Copy link
Contributor

basarat commented May 31, 2016

A boolean value should always be true or false, but never null (even if it's a falsy value).

Please compile with strictNullChecks and it will be a compile error 🌹

@colltoaction
Copy link
Author

I just tried adding it to my tsconfig file without luck (but don't worry, I'll figure it out). Anyway, adding this to my own project won't help if other dependencies are not using it, right?

@kitsonk
Copy link
Contributor

kitsonk commented May 31, 2016

@basarat forgot that strictNullChecks is only in the pre-release version of TypeScript (typescript@next)

The challenge here is that TypeScript before 2.0 (which is in pre-release) did not considered all types to be inclusive of null and undefined to reflect the run-time behaviour of JavaScript. With strictNullChecks, those types are removed from other types and guarded against.

TypeScript is not changing the runtime behaviour of the code you wrote (which effectively

But you still have issues with the way you wrote your code, because there are several cases where JavaScript at runtime will short-circuit your logic and return something other than a boolean:

console.log(bool_fun(null));
console.log(bool_fun(undefined));
console.log(bool_fun(0));
console.log(bool_fun(''));

Of course the third one would be caught by TypeScript today and the first and second one with strictNullChecks but the forth one will pass all the type guarding and it is a tough logic error to do static analysis on in TypeScript. So if you always want something that returns a boolean, write it so it always returns a boolean and don't let runtime JavaScript implicit coercions (or lack there of) ruin your runtime behaviour:

function bool_fun(value: string) : boolean {
    return Boolean(value && value == "bug");
}

@colltoaction
Copy link
Author

Thanks to both, that's very helpful!

On Tue, May 31, 2016, 06:19 Kitson Kelly notifications@github.com wrote:

@basarat https://github.com/basarat forgot that strictNullChecks is
only in the pre-release version of TypeScript (typescript@next)

The challenge here is that TypeScript before 2.0 (which is in pre-release)
did not considered all types to be inclusive of null and undefined to
reflect the run-time behaviour of JavaScript. With strictNullChecks,
those types are removed from other types and guarded against.

TypeScript is not changing the runtime behaviour of the code you wrote
(which effectively

But you still have issues with the way you wrote your code, because there
are several cases where JavaScript at runtime will short-circuit your logic
and return something other than a boolean:

console.log(bool_fun(null));console.log(bool_fun(undefined));console.log(bool_fun(0));console.log(bool_fun(''));

Of course the third one would be caught by TypeScript today and the first
and second one with strictNullChecks but the forth one will pass all the
type guarding and it is a tough logic error to do static analysis on in
TypeScript. So if you always want something that returns a boolean, write
it so it always returns a boolean and don't let runtime JavaScript implicit
coercions (or lack there of) ruin your runtime behaviour:

function bool_fun(value: string) : boolean {
return Boolean(value && value == "bug");
}


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#8892 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ABeg9EbRb2NQ9iWb-Klut7NCSwR087cCks5qG_0ygaJpZM4IqR08
.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label May 31, 2016
@mhegazy mhegazy closed this as completed May 31, 2016
@mhegazy mhegazy added Suggestion An idea for TypeScript and removed Question An issue which isn't directly actionable in code labels Jun 8, 2016
@mhegazy mhegazy reopened this Jun 8, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2016

We can do better here. #8949 contains the fix.

@mhegazy mhegazy added this to the TypeScript 2.0 milestone Jun 8, 2016
@mhegazy mhegazy added the Committed The team has roadmapped this issue label Jun 8, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jun 8, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants