I have these definitions:
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
}
}
}
The
frommethod has multiple signature as shown above. I want to write the jsdoc comment on it for vscode intellisense using.
I have two ways of it, but neither of them is worked:
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
}
}
}
I have now a workround, but it's not an overloading, since the parameter x can not be prompted with exact type.
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
}
}
}
So is there a way to write correct overloading of function signature in tsdoc and how to write it if yes? Thank you.