我正在使用Octokit REST API来获取我的仓库中的克隆。我正在尝试使用时间戳进行遥测。当我调用getClones() API时, VS 代码告诉我这个TypeScript 交集类型是返回类型:
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 存储库的问题提交吗?