镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Boolean function returns null #8892

Description

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

Activity

  1. basarat commented on May 31, 2016

    @basarat
    Contributor

    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 🌹

  2. colltoaction commented on May 31, 2016

    @colltoaction
    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?

  3. kitsonk commented on May 31, 2016

    @kitsonk
    Contributor

    Basarat Ali Syed (@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");
    }
  4. colltoaction commented on May 31, 2016

    @colltoaction
    Author

    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 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
    .

  5. added
    SuggestionAn idea for TypeScript
    and removed
    QuestionAn issue which isn't directly actionable in code
    on Jun 8, 2016
  6. mhegazy commented on Jun 8, 2016

    @mhegazy
    Contributor

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

  7. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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