Skip to content

False-positive "Property does not exist" error in an Object.prototype.hasOwnProperty check block #56549

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Malix-Labs opened this issue Nov 26, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Malix-Labs
Copy link

πŸ”Ž Search Terms

Object Property, hasOwnProperty, if, check, block, scope, error, false-positive

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about the above tags

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYewdgzgLgBCBGArApsWBeGBvAUDfMADgE4iHLFQCeAjAFwwBEyAHgJbRtgDmjOAvjhxsAZjAAUeAgHoAVLKkFZMAErIoAV2JgGAMQCGAGwjJF+WdLMIUaAHQALfRADyAdzAAFUuUpVxAchIyCmoAJn8AShwI7DM5BQJzGABRFnI0ZAATBgA5EBTiUmJbMxhlNU1tBkZkwpBiBi9g3xhA7xCqcJhMkGQIGDAQWFYOWHAYanJWrCJ233oYaGIubgBuGH5-WxgAETZMmCoQDRgAW2R9MFagn2oafwB+cVCAVheaCL5EsstE0EgQIZkLZDCBuOJrKgoLYbh1QhFVgIcEA

πŸ’» Code

const object = {
    property1: "existing"
}

if (
    /**
     * Return: False
     */
    object.hasOwnProperty('property2')
) {
    /**
     * Expected: No Error.
     * Return: "Error: Property 'property2' does not exist on type '{ property1: string; }'. Did you mean 'property1'?(2551)"
     */
    console.log(object.property2);
}

πŸ™ Actual behavior

Error: Property 'property2' does not exist on type '{ property1: string; }'. Did you mean 'property1'?(2551)

πŸ™‚ Expected behavior

No error

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 26, 2023

Duplicate of #41915. Used search terms: hasOwnProperty in:title

Currently hasOwnProperty can not be used to narrow the type of a variable.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 27, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants