Skip to content

Type guard/Equality narrowing doesn't work on "any"Β #54239

Open
@kkmuffme

Description

@kkmuffme

Bug Report

πŸ”Ž Search Terms

type guard equality narrowing strict equal equality

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?#code/FAMwrgdgxgLglgewgAgEYEMBOAKZWDmAXHhAJ7ICUyA3sMvcnCMrgcgLyfIRgA2vlGnQYjMAUxhhMKAgG5h9AL7AFjZq0z4OXEOl4BnMYNojREqTM3yRy5SqA

πŸ’» Code

function bar( arg: any ) {
    if ( arg === null ) {
        return arg;
    }

    if ( arg === false ) {
        return arg;
    }
}

πŸ™ Actual behavior

arg should be null/false inside the if conditions

πŸ™‚ Expected behavior

arg is any

The docs say this should work https://www.typescriptlang.org/docs/handbook/2/narrowing.html
When using unknown instead of any it works perfectly fine. I think adjusting this, so it works for any too, isn't too far fetched.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions