You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JSDoc实现与指定TypeScript组件获取方法等价的代码?

将TypeScript泛型方法转换为等价JSDoc版本

原TypeScript方法示例

get_components<T extends Component>(component_type_to_return: { new (doodad: Doodad): T }): T[]
{
    return this.components.filter(component => component instanceof component_type_to_return)
}

等价的JSDoc实现方案

/**
 * 如果Doodad已挂载对应类型组件,则返回该类型的组件。
 * @template    {Component} T
 * @param       {{ new (doodad: Doodad): T }}   type    要返回的组件类型
 * @return      {T[]}                                   挂载在Doodad上的所有指定类型组件
 */
get_components(type)
{
    return this.components.filter(component => component instanceof type)
}

内容的提问来源于stack exchange,提问作者Björn Hjorth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 16:39:46