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.

Object.getPrototypeOf returns `any` instead of `object | null`

See original GitHub issue

TypeScript 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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
m93acommented, Mar 27, 2018

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?

1reaction
millspcommented, Dec 27, 2018

@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 !

Read more comments on GitHub >

github_iconTop 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 >

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