Suggestion for specifying a class static side interface that supports derived classes and type parameters #33916
Closed
5 tasks done
Labels
Duplicate
An existing issue was already created
Search Terms
class static side interface extend
Problem
Currently, there's no way to set the static side interface of a class. This is something dating back to #1263 and has been a topic more recently in #17545.
Some suggest adding static properties on interfaces. That doesn't solve the problem in a reasonable manner, I think. Other suggestions propose defining the class with an expression, but that doesn't completely solve the problem either. Consider this:
I can enforce that
Foo
has the staticlen
. But I want to extend that class too, and the derived classes should have this property as well. Currently, this can't happen:You might suggest to simply put the
len
property in the class itself and not use an interface in the first place:In this case, that would work, but suppose we have a more advanced use case with type parameters:
Now, you are forced to use a class expression because defining the static property as earlier is not possible:
This error makes sense because the
len
inFoo
can be of any type, and its implementation probably expects a certain type. As said in #33870.Suggestion
We need some sort of way to attach a static side interface to the class itself, not just the resulting variable of an expression. I believe this can be solved with the suggestion in #33892:
As demonstrated, the specified static type in
Foo
now has a type parameter, but it is used as a value. Unlike the problem earlier,len
inFoo
now has a determined type,number
.Derived classes of
Foo
should have the static side ofFoo
enforced on them, requiringlen
ofBar
to be a number. However, if the derived class declares its own static side, it is merged on top of the parent's static side. That's whylen
inBar2
is expected to bestring
.Examples
Basically, to set the instance side of a class, you use
implements
. To set the static side, you assign a type to its symbol:Use Cases
Essentially, this solves three issues:
constructor
Suggestion: Improve type ofconstructor
on the instance type of a class #32452 (edit)Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: