Repository navigation
Boolean function returns null #8892
Description
Activity
A boolean value should always be true or false, but never null (even if it's a falsy value).
Please compile with
strictNullChecksand it will be a compile error 🌹I just tried adding it to my
tsconfigfile 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?Basarat Ali Syed (@basarat) forgot that
strictNullChecksis 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
nullandundefinedto reflect the run-time behaviour of JavaScript. WithstrictNullChecks, 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
strictNullChecksbut 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"); }
Thanks to both, that's very helpful!
On Tue, May 31, 2016, 06:19 Kitson Kelly notifications@github.com wrote:
Basarat Ali Syed (@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 effectivelyBut 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
.- addedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
on May 31, 2016 - addedSuggestionAn idea for TypeScriptAn idea for TypeScriptand removedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
on Jun 8, 2016 We can do better here. #8949 contains the fix.
- addedCommittedThe team has roadmapped this issueThe team has roadmapped this issue
on Jun 8, 2016 - addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Jun 8, 2016 - locked and limited conversation to collaborators
on Jun 19, 2018
TypeScript Version:
1.8.10
Code
A
booleanvalue should always betrueorfalse, but nevernull(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.2Expected behavior:
Either we have a compiler error or the console prints:
Actual behavior:
The console prints: