Allow arbitrary expressions in 'extends' clauses #511
Labels
ES6
Relates to the ES6 Spec
Fixed
A PR has been merged for this issue
Suggestion
An idea for TypeScript
Milestone
The language currently only allows dotted identifiers in
extends
clauses, e.g.class Foo extends X.Y.Bar { }
. The identifier must resolve to a class in both the value and type namespaces.With the advent of class expressions (#497), we'll presumably need to start allowing arbitrary expressions, or at least loosen up the restriction that the named parent be an actual class.
The question is, given the type of
B
, how do we determine the type ofA
? Any rule should ideally end up at the same result as the current type system whenB
is a class.Construct signatures?
Rule
The simplest rule would be that the return type of the construct signature of
B
is the prototypal parent ofA
. The constructor ofA
must invoke that construct signature (viasuper
) according to the same rules as usual.Issues
Problematically,
B
can actually have any number of construct signatures, with no restriction:We cannot use
A
'ssuper
call to resolve the ambiguity via overload resolution becauseA
might be an ambient class, in which case we won't know whichsuper
overload was selected.Prototype?
Rule
TypeScript defines the
prototype
property of a class constructor function to be the same as the instance shape of the class. This is the prototypal parent ofA
, andA
's constructor must call one ofB
's construct signatures viasuper
.Issues
The
prototype
property replaces all references toA
's type parameters withany
, erasing the genericness of the type.Decision points
Questions to consider at this point:
prototype
property? Probably not.Compromise
Rule
A class extending some type
B
has a prototypal parent of the best common type of the return types ofB
's construct signatures. This best common type must exist (not{}
), and all ofB
's construct signatures must have same number of generic type parameters. The extending class must specify that same number of generic type arguments when referencingB
in theextends
clause, which are then applied to the BCT ofB
's construct signature. Otherwise, it is an error to attempt to extend fromB
.If a
super
call is required, the extending type's constructor may invoke any construct signature of the base type.TBD
The text was updated successfully, but these errors were encountered: