Skip to content

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

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

Open
jeff3dx opened this issue May 11, 2018 · 1 comment
Open
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@jeff3dx
Copy link

jeff3dx commented May 11, 2018

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:

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label May 11, 2018
@RyanCavanaugh
Copy link
Member

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

@mhegazy mhegazy added Suggestion An idea for TypeScript and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels May 11, 2018
@mhegazy mhegazy added this to the Community milestone May 11, 2018
@mhegazy mhegazy added the Help Wanted You can do this label May 11, 2018
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants