If there is a way to add this type information in some extra file that doesn't add any typescript to the existing code then that would be fine.
TypeScript supports JSDoc annotations, so you could actually achieve this.
Some thing like this:
JSDoc code sample
```javascript
/**
* Generate CFI string from node and offset.
*
* @param {Node | { node: Node, offset?: number }[]} node
* @param {number} [offset]
* @param {string} [extra]
* @returns {string}
*/
static generate(node, offset, extra) {
/** @type {string} */
let cfi;
if (node instanceof Array) {
let strs = [];
for (let o of node) {
strs.push(this.generatePart(o.node, o.offset));
}
cfi = strs.join('!');
} else {
cfi = this.generatePart(node, offset, extra);
}
if (extra) cfi += extra;
return `epubcfi(${cfi})`;
}
```
You can generate a .d.ts type generation file so users can have type completion and auto-suggestion benefits.
JSDoc is a bit painful to write than in TypeScript, but it's a perfect harmony for both parties; those who do not want to author in TypeScript & for people who want to have type definition support.
Actually, I have done most of the part, on pursuit of trying to understand the code, except for some confusions on this.from and this.to -- can't really understand which type they are supposed to be.
If you are interested, and help me understand the code a little bit, maybe I can submit a PR for it.
This is related to #6.
TypeScript supports JSDoc annotations, so you could actually achieve this.
Some thing like this:
JSDoc code sample
```javascript /** * Generate CFI string from node and offset. * * @param {Node | { node: Node, offset?: number }[]} node * @param {number} [offset] * @param {string} [extra] * @returns {string} */ static generate(node, offset, extra) { /** @type {string} */ let cfi; if (node instanceof Array) { let strs = []; for (let o of node) { strs.push(this.generatePart(o.node, o.offset)); } cfi = strs.join('!'); } else { cfi = this.generatePart(node, offset, extra); } if (extra) cfi += extra; return `epubcfi(${cfi})`; } ```You can generate a
.d.ts
type generation file so users can have type completion and auto-suggestion benefits.JSDoc is a bit painful to write than in TypeScript, but it's a perfect harmony for both parties; those who do not want to author in TypeScript & for people who want to have type definition support.
Actually, I have done most of the part, on pursuit of trying to understand the code, except for some confusions on
this.from
andthis.to
-- can't really understand which type they are supposed to be.If you are interested, and help me understand the code a little bit, maybe I can submit a PR for it.