Tengo estas definiciones:
class Entity {} class Point {} class Edge {} class A { from(x, y) { if (x == null) { return this.remember('_from') } else if (x instanceof Entity || x instanceof Point) { this.remember('_from', new Edge(x)) return this } else { this.remember('_from', new Edge(new Point(x, y))) return this } } }El método
fromtiene varias firmas, como se muestra arriba. Quiero escribir el comentario jsdoc sobre él para usar vscode intellisense.
Tengo dos formas de hacerlo, pero ninguna de ellas funciona:
class A { /** * @type {((x: number, y: number) => this) & ((x: Entity|Point) => this) & (() => Edge)} */ from(x, y) { if (x == null) { return this.remember('_from') } else if (x instanceof Entity || x instanceof Point) { this.remember('_from', new Edge(x)) return this } else { this.remember('_from', new Edge(new Point(x, y))) return this } } } class A { /** * @template T * @typedef {{ * (this: T, x: number, y: number): T; * (this: T, entity: Entity): T; * (this: T, point: Point): T; * (): Edge; * }} fromType * * @type {fromType<this>} */ from(x, y) { if (x == null) { return this.remember('_from') } else if (x instanceof Entity || x instanceof Point) { this.remember('_from', new Edge(x)) return this } else { this.remember('_from', new Edge(new Point(x, y))) return this } } }Ahora tengo una solución alternativa, pero no es una sobrecarga, ya que el parámetro x no se puede solicitar con el tipo exacto.
class A { /** * @template {unknown} T * @param {T} [x] * @param {number} [y] * @returns {T extends Entity|Point|number ? this : Edge} */ from(x, y) { if (x == null) { return this.remember('_from') } else if (x instanceof Entity || x instanceof Point) { this.remember('_from', new Edge(x)) return this } else { this.remember('_from', new Edge(new Point(x, y))) return this } } }Entonces, ¿hay alguna manera de escribir la sobrecarga correcta de la firma de la función en tsdoc y cómo escribirla en caso afirmativo? Gracias.