hasOwnProperty return type
See original GitHub issueSearch 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
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:
- Created 3 years ago
- Reactions:8
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.PS: if this can be ‘fixed’ for
'testProp' in obj
that would be amazing too.