Microsoft/TypeScript

Object.constructor isn't implemented specifically enough

Open

#4,586 建立於 2015年9月1日

在 GitHub 查看
 (4 留言) (7 反應) (0 負責人)TypeScript (48,455 star) (6,726 fork)batch import
BugDomain: lib.d.tsHelp Wanted

描述

Consider:

class Foo extends ([].constructor) {
}

[].constructor should be Array, which we aught to be able to extend. However, the checker complains on [].constructor that "Type 'Function' is not a constructor function type". Considering .constructor is quite explicitly the constructor function for the object, I believe this is a lie perpetuated by our lib.d.ts files. I think we need to go add appropriate self-constructor-referencing .constructor fields to each builtin.

Additionally consider the following:

class Bar {
}

class Foo extends ((new Bar()).constructor) {
}

The same thing but with a custom class rather than a builtin. We should know the correct type of .constructor here (the Bar class), but we don't - we just look at the members on Object.

In the first example, I believe it could be fixed with more precise lib files, but in the second case, I believe this is an instance of a field whose type should be known by the compiler, much like .prototype on the class itself is known.

貢獻者指南

Object.constructor isn't implemented specifically enough · Microsoft/TypeScript#4586 | Good First Issue