question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hasOwnProperty return type

See original GitHub issue

Search Terms

hasOwnProperty

Suggestion

interface Object {
    hasOwnProperty<Key extends PropertyKey>(v: Key): this is this & Record<Key, unknown>;
}

Use Cases

To be able to use the property of an object after it has been tested for its existence. Current behavior:

// Property 'testProp' does not exist on type 'object'.(2339)
obj.hasOwnProperty('testProp') && typeof arg.testProp === 'string' 

Examples

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgPICMBWEFmQbwFgAoZM5AeguQGEB7AWwYnGToFc8wALYAZ2QAbUCjB1kfCCnTtggsAFpQydnzgBzCADoS5ZNzh9UAdxAAFKHQAO0MAE8APAGkId5BAAekEABMBF61s7FzsAPgAKADcALmQQgEpYnn5kFOSBADJkACUcOigfZ1cAGhUQAGsQOlNQgG4SAF8SEhh2EFxgOhBkSD4wcLgodVi2yuqQeIJdcmAYZHDm0j1yexs6OcH1ZABeXeQAcjosHDB96eXkDKzN5ABCPZB2QUFz5avkTa0DI1MAmyh7OF9r0wH99vFXnp3qsIOsPkMtCC-js9vs+lBQOp9pRqAA5fIMOCCdxQSxQWJ-IIHJGWKzYnx0CACKp4Tz8PBdHp2GwHI7YXD7LThABMAGZRQBOCHEV6TIhLC7GODgPgAZTAGJA6gGCJp1ni9QVyCaxBNLTaHU5IOFOuGZTGpjlr1m80WFzIMLhN12215xwFkPI7xu919j2egbI72BTNBtOxyk2kcuWU9G11seRPoO6Mx2KoyFVcGYJLJFNpVJjfTByAZTOQLPcHnZbG6ML9-NOQrFkulsqmRr0SpV6s12s+eqsBteJrNxFa7TAnW6w7Aao1mPC6Niua1cuQUAgYHYUG66NqxtqQA

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
WORMSScommented, Dec 10, 2020

Duplicate of #18282.

You say it’s a duplicate, but it seems their example was a far inferior attempt? None of the examples in #18282 would be as seamless as this is this & Record<Key, unknown> Maybe due to 3 years ago when TS was not as mature.

1reaction
WORMSScommented, Dec 10, 2020

PS: if this can be ‘fixed’ for 'testProp' in obj that would be amazing too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object.hasOwn() - JavaScript - MDN Web Docs
The Object.hasOwn() method returns true if the specified property is a direct property of the object — even if the property value is...
Read more >
JavaScript hasOwnProperty() Method - GeeksforGeeks
Return Value: It returns a boolean value indicating whether the object has the given property as its own property.
Read more >
What is property in hasOwnProperty in JavaScript?
Object.hasOwn() is a static method which returns true if the specified object has the specified property as its own property. If the property...
Read more >
Javascript hasownproperty method - Flexiple
This method is used to check whether the property on an object belongs to the mentioned object or not. If the property belongs...
Read more >
What is the hasOwnProperty function in JavaScript?
The hasOwnProperty function returns a boolean value. If the property belongs to the object being tested, true is returned. Otherwise, false is returned....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found