google / google/closure-compiler
How to describe classes with statics in a return type
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Say I want a factory which returns a class with certain static members. How can I write the return type? The constructor shape can be described by `function(new: Type, args)`, but what if we also want to specify some static members?
To demonstrate, we'll define an interface for 2d `Point`s. `makeHorizontalLine()` will return a `Point` class, with a static member `Intercept`.
```js
/** @interface */
class Point { /** @type {number} */ x; /** @type {number} */ y; }
/**
* @param {number} y
* @return {typeof PointShape}
*/
function makeHorizontalLine(y) {
return /** @type {typeof PointShape} */(
/** @implements {Point} */
class {
/** @param {number} x */
constructor(x) {
this.x = x;
this.y = y;
}
static Intercept = new this(0);
}
)
}
class PointShape {
/** @param {number} x */
constructor(x) { }
/** @type {Point} */
static Intercept; // where this line intersects the x=0 line
}
```
This one seems to work but it's quite indirect, requires a cast and I'm not sure if it's sound for property mangling. In typescript an interface containing a constructor type (`function(new: Type, args)`) is understood as describing the shape of the class, not the instances. What options are there in Google Closure?
Contributor guide
Research direction
Start by tracing how the shown JSDoc forms—`typeof PointShape`, constructor types, and static members—are interpreted. Define the desired class/static return-type semantics and check soundness with the Point example, including property mangling. Done means the supported option or compiler change is clearly scoped and documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100