Object.getPrototypeOf returns `any` instead of `object | null`
See original GitHub issueTypeScript Version: 2.7.2
Search Terms: Object.getPrototypeOf
, ObjectConstructor.getPrototypeOf
, wrong return type
Code
let prototype = Object.getPrototypeOf(foo);
Expected behavior:
The type of prototype
should be object | null
. According to the spec, getPrototypeOf
returns the value of the internal property [[Prototype]]
[1] whose values can only be of type object
or null
[2].
Actual behavior:
prototype
has type any
Playground Link: here
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Object.getPrototypeOf returns any instead of object | null #22875
The type of prototype should be object | null . According to the spec, getPrototypeOf returns the value of the internal property [[Prototype]]...
Read more >Why does getPrototypeOf 'null' give an error? - Stack Overflow
It's true that typeof null evaluates to 'object' , but it actually came from a limitation and may be considered a quirk -...
Read more >Object.getPrototypeOf() - JavaScript - MDN Web Docs - Mozilla
getPrototypeOf () static method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.
Read more >A Better Method of Type Checking JavaScript Objects - Medium
getPrototypeOf (). We all know JavaScript's typeof keyword has some weird behavior, like the typeof a null value returning ...
Read more >What is the Object.getPrototypeOf method in JavaScript?
Learn the 24 patterns to solve any coding interview question without getting lost in a ... If the passed object has no prototype,...
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 Free
Top 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
The best scenario would be if TypeScript kept track of the prototype chain of its types – then
Object.getPrototypeOf
could just return the type of[[Prototype]]
, probably breaking much less code. But I doubt TypeScript keeps track of the type of__proto__
property on various objects, or does it?@RyanCavanaugh We could add a new compiler flag to manually enable this fix (to stay non-breaking). Then anything missing (like Reflects) could also be added… It is very much used & it would be more than welcome !