Microsoft/TypeScript

Object.constructor isn't implemented specifically enough

Open

#4 586 ouverte le 1 sept. 2015

Voir sur GitHub
 (4 commentaires) (7 réactions) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: lib.d.tsHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

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.

Guide contributeur