-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
Please compile with |
I just tried adding it to my |
@basarat forgot that The challenge here is that TypeScript before 2.0 (which is in pre-release) did not considered all types to be inclusive of 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 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:
|
We can do better here. #8949 contains the fix. |
TypeScript Version:
1.8.10
Code
A
boolean
value should always betrue
orfalse
, 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:
The text was updated successfully, but these errors were encountered: