Skip to content

Type guard does not work when its value is set to a variable. #19943

Closed

Description

TypeScript Version: 2.7.0-dev.20171111

Code

// -- Errors here:
test(value: number | undefined) : number {
    let isNumber = typeof value == "number";
    if (isNumber && value > 0) // < -- TS2532: Object is possibly 'undefined'.
        return value * 10;

    return 0;
}

// -- No errors here:
test(value: number | undefined) : number{
    if (typeof value == "number" && value > 0)
        return value * 10;

    return 0;
}

Expected behavior:
No errors

Actual behavior:

TS2532: Object is possibly 'undefined'.

Activity

  1. ahejlsberg commented on Nov 11, 2017

    @ahejlsberg
    Member

    Duplicate of #12184.

  2. typescript-bot commented on Nov 27, 2017

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  3. locked and limited conversation to collaborators on Jun 14, 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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions