Skip to content
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

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

Closed
alexandretperez opened this issue Nov 11, 2017 · 2 comments · Fixed by #44730
Closed

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

alexandretperez opened this issue Nov 11, 2017 · 2 comments · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@alexandretperez
Copy link

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

@ahejlsberg
Copy link
Member

Duplicate of #12184.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Nov 11, 2017
@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants