如何解决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 举报,一经查实,本站将立刻删除。