Skip to content

lib.es2022: hasOwn appears to be defined on Object prototype rather than on constructorΒ #48206

Description

@lediur

Bug Report

πŸ”Ž Search Terms

hasOwn, Object.hasOwn, es2022, prototype, lib.es2022.object.d.ts

πŸ•— Version & Regression Information

  • This changed between versions 4.2.4 and 4.6.2

Appears to be introduced by #46291

⏯ Playground Link

Doesn't appear to reproduce on Playground or Bug Workbench, reproduces in VS Code 1.66.0-insider (e80a0ca) running TypeScript 4.6.2.

πŸ’» Code

interface SomeComponentProps {
  requiredProp: boolean
  hasOwn?: boolean;
}

const SomeComponent: React.FC<SomeComponentProps> = () => {}

return <SomeComponent requiredProp />

πŸ™ Actual behavior

I ran into this issue because I was trying to upgrade the project from 4.3.5 to 4.6.2. The project has some object types with hasOwn as a boolean property.

// TS2322: Type '{ requiredProp: boolean }' is not assignable to type 'SomeComponentProps'.
  Types of property 'hasOwn' are incompatible.
    Type '(o: object, v: PropertyKey) => boolean' is not assignable to type 'boolean'.

πŸ™‚ Expected behavior

hasOwn should be a method of the Object constructor (Object.hasOwn), not the prototype (foo.hasOwn).

Activity

  1. RyanCavanaugh commented on Mar 10, 2022

    @RyanCavanaugh
    Member

    This is an intended UX trade-off in JS files. Since often you'll have untyped variables, we show method help based on name-matching; this way if you're looking for help for a common method name, it shows up even without matching type information.

    For more precise behavior, you can turn on checkJs.

    See also #33565

  2. lediur commented on Mar 10, 2022

    @lediur
    Author

    Thanks for the reply! I'm seeing this in a TypeScript file in a TypeScript React project when trying to upgrade from 4.3.5 to 4.6.2. Does the same behavior apply?

    Let me get a more specific example of the issue I'm running into, one sec.

  3. lediur commented on Mar 10, 2022

    @lediur
    Author

    I ran into this issue because I was trying to upgrade the project from 4.3.5 to 4.6.2. The project has some object types with hasOwn as a boolean property.

    e.g.

    interface SomeComponentProps {
      requiredProp: boolean
      hasOwn?: boolean;
    }

    Using those types started failing to typecheck after the upgrade since TypeScript. For example:

    const SomeComponent: React.FC<SomeComponentProps> = () => {}
    
    return <SomeComponent requiredProp />
    // TS2322: Type '{ requiredProp: boolean }' is not assignable to type 'SomeComponentProps'.
      Types of property 'hasOwn' are incompatible.
        Type '(o: object, v: PropertyKey) => boolean' is not assignable to type 'boolean'.
  4. RyanCavanaugh commented on Mar 10, 2022

    @RyanCavanaugh
    Member

    No, you're totally right, this is just the lib being wrong. Misread what you were getting at.

  5. lediur commented on Mar 10, 2022

    @lediur
    Author

    No problem! I should've provided the more specific error case up front.

  6. lediur commented on Mar 10, 2022

    @lediur
    Author

    I'm happy to make a PR with test cases for the fix, though it might take me a bit longer since I haven't contributed to TypeScript before. If you'd like to fix this internally that's fine by me.

  7. RyanCavanaugh commented on Mar 10, 2022

    @RyanCavanaugh
    Member

    I'll put up a PR. Thanks for the report πŸ‘

  8. added a commit that references this issue on Mar 10, 2022
    d1b063e
  9. added a commit that references this issue on Mar 10, 2022
    7ac565f
  10. added a commit that references this issue on Mar 10, 2022
    a4f5555
  11. added a commit that references this issue on Mar 11, 2022
    e397ed1
  12. lediur commented on Mar 16, 2022

    @lediur
    Author

    Do you know if or when this might land in a patch version?

  13. RyanCavanaugh commented on Mar 16, 2022

    @RyanCavanaugh
    Member

    We'll be doing a patch release in the next week or two (just waiting to see if other patchworthy things appear first)

  14. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions