api-typings icon indicating copy to clipboard operation
api-typings copied to clipboard

[Bug]selectAll和select两个选择器的返回值应该是不一样的

Open Lienviws opened this issue 4 years ago • 6 comments
trafficstars

环境

  • api-typings 版本:3.3.2
  • 系统版本:macos 10.14.6
  • ide: vscode 1.57.0

问题

在使用 selectAll时,boundingClientRect cb 里面的 results 是个对象,而不是数组。

wx.createSelectorQuery().selectAll('.a-class').boundingClientRect(function(rects){
  rects // d.ts 内是个对象而不是数组
}).exec()

证明它是个问题

根据小程序文档可知:

wx.createSelectorQuery().select.boundingClientRect(cb)

wx.createSelectorQuery().selectAll.boundingClientRect(cb)

内的cb类型实际上是不同的。

前者内的 cb 类型类似这样

(result: BoundingClientRectCallbackResult) => void

而后者的cb类型应该是这样

(results: BoundingClientRectCallbackResult[]) => void

但是在 lib.wx.api.d.ts中,返回的都是 NodesRef 类型,没有区分对象和集合

select(
  /** 选择器 */
  selector: string
): NodesRef

selectAll(
  /** 选择器 */
  selector: string
): NodesRef

Lienviws avatar Jun 18 '21 11:06 Lienviws

同 #194 #195 这两个问题。但它们似乎没有更多的下文了,就新开一个 issues 描述的详细一点

Lienviws avatar Jun 18 '21 11:06 Lienviws

这个接口设计得不是很 ts……手动改好像容易些,基于现在从文档生成的流程好像有些困难,我看看是不是先改成 result | result[] 做个 workaround

SgLy avatar Jul 05 '21 08:07 SgLy

加个泛型呗

Mister-Hope avatar Jul 05 '21 08:07 Mister-Hope

满屏as 莫不如result[] 的时候强制 as unknown as result[]了

Mister-Hope avatar Jul 05 '21 08:07 Mister-Hope

+1

richex-cn avatar May 19 '22 07:05 richex-cn

2023年了, 问题依旧, 请问至少有没有什么可接受点的workaround? 靠<WechatMiniprogram.BoundingClientRectCallbackResult[]><unknown>总有点 satisfy complier only 的感觉

Reginald-Yoeng-Lee avatar Jun 07 '23 11:06 Reginald-Yoeng-Lee