javascript - 在Javascript文件中,如何在tsdoc/jsdoc中注释重载函数签名(可以被vscode智能识别)
问题描述
我有这些定义:
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
}
}
}
该
from
方法具有多个签名,如上所示。我想为 vscode 智能感知写 jsdoc 评论。
我有两种方法,但它们都不起作用:
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
}
}
}
我现在有一个工作区,但它不是重载,因为参数 x 不能以确切的类型提示。
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
}
}
}
那么有没有办法在 tsdoc 中编写正确的函数签名重载以及如何编写它?谢谢你。
解决方案
推荐阅读
- java - 如何并排打印一副卡片?
- python - BeautifulSoup:如何提取
- 一个内的信息
- flutter - Flutter:在子小部件的状态上调用函数
- c# - ActionFilterAttribute 内的 aspnet core mvc 访问依赖指示服务
- machine-learning - 您如何区分与神经网络中的权重相关的误差?
- ios - 从 API 传递的图像未在页面浏览容器中快速显示
- c++ - 构造函数中的weak_from_this()
- javascript - Issue with storing and recalling cookies Javascript
- react-native - onPress 功能不起作用
- sql-server - 如何从本地实例查询/插入 - Azure SQL Server