'Using JSDoc to set an index signature on an ES6 Class
In Typescript, you'd just do this:
class Test {
[key: string]: whatever
}
Which allows you to access computed property names like so...
class Test {
getProp(key) {
return this[key]
}
}
... without receiving Element implicitly has an 'any' type because type 'Test' has no index signature.
I can't figure out how to accomplish the equivalent with JSDoc. Anyone had any luck with this?
Solution 1:[1]
For a single method, it's possible to do this with the @this tag:
class Test {
/**
* @this {{[k: string]: string}}
* @param {string} key
*/
getProp(key) {
return this[key]
}
}
Doing it for the entire class isn't yet supported. There is an open feature request (TypeScript#48096) which is currently in the TS 4.7.1 milestone. However, given the Needs Proposal label, somebody needs to come up with a proposed JSDoc tag + syntax, or it will likely end up being pushed off that milestone.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gerrit0 |
