Skip to content

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

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
kkmuffme opened this issue May 13, 2023 · 2 comments
Open

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

kkmuffme opened this issue May 13, 2023 · 2 comments
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@kkmuffme
Copy link

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.

@jcalz
Copy link
Contributor

jcalz commented May 13, 2023

References:

@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label May 15, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 15, 2023
@RyanCavanaugh
Copy link
Member

I don't know why you wouldn't just write false or null in those cases instead of writing the argument name again, but I guess it would be more correct to do this for primitives at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

3 participants