Skip to content

Typescript does not recognize function Boolean() as a check for undefined #24063

Description

@jeff3dx

I followed the submission guidelines.

I prefer to check for a defined object using Boolean(object) rather than !!object, which I consider bad style. Typescript linter recognizes !! as a check for undefined but does not recognize Boolean() as a check for undefined.

I could not find a tsconfig or tslint configuration setting related to this behavior.

2.9.0-dev.20180511 (typescript@next)

"Boolean" "Boolean(" "function Boolean"

Code
This is not production code but a simple demonstration only to satisfy the expression tree.

function booleanTest() {
    let value: Array<object> | undefined = undefined;

    if (Date.now() > 1) {
        value = [{}];
    }

    if (Boolean(value)) {
        console.log(value.length);
    }
}

Expected behavior:
No Typescript linter errors

Actual behavior:
Typescript linter in VS Code displays an error "[ts] Object is possibly 'undefined'." with a red squiggly on the final reference to variable "value".

Playground Link:

Activity

  1. RyanCavanaugh commented on May 11, 2018

    @RyanCavanaugh
    Member

    Mohamed Hegazy (@mhegazy) we could declare it as <T>(obj: T | false | undefined | null | 0 | ""): obj is T; ?

  2. added
    SuggestionAn idea for TypeScript
    and removed
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    on May 11, 2018
  3. added this to the milestone on May 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions