Octokit REST API getClones 返回奇怪的 TypeScript 类型

问题描述

我正在使用 Octokit REST API获取我的存储库中的克隆。我正在尝试使用时间戳进行遥测。当我调用 getClones() API 时,VS 代码告诉我这个 TypeScript intersection type 是返回类型:

clones: {
    count: number;
    uniques: number;
    clones: {
        timestamp: string;
        uniques: number;
        count: number;
    }[];
} & {
    timestamp: string;
    uniques: number;
    count: number;
}[]

问题是,当我随后尝试 .map() 该数组时,VS 代码认为每个元素不是交集类型,而是后半部分那个路口。示例:

    //clones here is of the intersection type I pasted above ^^
    const clones = await octokit.api.paginate(octokit.api.repos.getClones,{
      owner: repo.owner,repo: repo.name,per_page: 100,state: 'all',mediaType: {
        previews: ['squirrel-girl'],},});

    const mappedClones = clones.map(clone => {
      /*
       VS Code thinks clone is of the below type,which is not the intersection type,but rather the
       second half of the intersection:

      (parameter) clone: {
          timestamp: string;
          uniques: number;
          count: number;
      }
      */
      
      //However,if I dump the object,it's actually the first type in that intersected type above
      console.log(`Repo: ${repo.name}\nClone:`,JSON.stringify(clone));
      //If I try to reference the clones property of this object,I get a typescript error
      const telemetry = clone.clones; //ERROR
    }

输出

Repo: myRepo
Clone: {"count":100,"uniques":90,"clones":[{"timestamp":"2021-05-11T00:00:00Z","count":1,"uniques":1},{"timestamp":"2021-05-12T00:00:00Z","count":28,"uniques":25},{"timestamp":"2021-05-13T00:00:00Z","count":35,"uniques":33},{"timestamp":"2021-05-14T00:00:00Z","count":36,"uniques":33}]}

我不确定这是否是 TypeScript 问题。我已经查看了类型代码,但是很难阅读各种泛型类型的所有间接和组合。欣赏任何见解。有什么解决方法吗?我可以以某种方式将 map() 函数中的类型强制为我想要的形状吗?甚至any

我应该将此作为针对 Octokit 存储库的问题提交吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...