I was coding some JavaScript modules trying to have the intellisense working on VsCode with the JsDocs
But now I’m struggling trying to write the JsDoc for the following case:
Suppose I have this classes
class SomeClass extends Array {}
class SomeOtherClass extends String {}
And I want to create this function to extend them
function WithFeature(base) {
return class extends base {
constructor() {
super(...arguments)
}
feature(){
// do something...
}
}
}
So I can make more cool classes
class CoolClass extends WithFeature(SomeClass) {
// ...
}
class OtherCoolClass extends WithFeature(SomeOtherClass) {
// ...
}
How can I JsDoc my beatifull WithFeature
?
I thought something like this would work
/**
* @template {T}
* @param {new () => T} base
* @returns {new () => T}
*/
function WithFeature(base) {
// ...
}
or maybe this
/** @type {<T>(base: T) => new () => T} */
function WithFeature(base) {
// ...
But of course it didn’t work for me (ie. it didn’t show the intellisense hints on VsCode) otherwise I wouldn’t asked this question!
Have somebody else encountered the same drama?
Edit
I noticed this that can be a step toward the solution!
class SomeClass extends Array {}
class SomeOtherClass extends String {}
/**
* @template T
* @returns {T}
*/
function WithFeature(/** @type {T} */ base) {
return class extends base {
feature(){
// do something...
}
}
}
class CoolClass extends WithFeature(SomeClass) {
// I can press ctrl-space to see the properties and methods
// from the SomeClass (and from Array) but not from WithFeature
}
class OtherCoolClass extends WithFeature(SomeOtherClass) {
// I can press ctrl-space to see the properties and methods
// from the SomeOtherClass (and from String) but not from WithFeature
}
But still I couldn’t find a way to see the hints of WithFeature
(ie. the feature()=>void
method)
I also tried /** @returns {{feature()=>void} & T} */
but still VsCode is showing me only the properties from the T
type but not the new feature
method from WithFeature
2
Answers
I think I can post an answer for my own question!
The key point is that because the argument of
WithFeature
is a constructor itself and the typeT
that I want to grasp is the type of the instance of such constructor, I need to pick the@template T
from the/** @type {new()=>T} */
of thebase
argument so that the method can@returns
a constructor of typenew () => (T & SomeOtherType)
.But this solution still is has some flaws...
To conclude, if you do
Then the IDE can show everything but the stuff added by
WithFeatureType
(so thefeature
method is not shown).Otherwise with
You can see both the stuff from
T
and fromWithFeatureType
but you lose the info about the arguments of the base constructor.Further investigations are needed!
you can use this format:
For class:
Reason for doesn’t showing
I suggest you to directly use the above method