Hello all,
TypeScript is doing my head in, by seemingly not understanding my apps conditions:
error: TS2345 [ERROR]: Argument of type '(pull: isMerged) => MergeCommit' is not assignable to parameter of type '(value: Pull, index: number, array: Pull[]) => MergeCommit'.
Types of parameters 'pull' and 'value' are incompatible.
Type 'Pull' is not assignable to type 'isMerged'.
Type 'isNotMerged' is not assignable to type 'isMerged'.
The types of 'pullRequest.merged' are incompatible between these types.
Type 'false' is not assignable to type 'true'.
?.map((pull: isMerged) => pull.pullRequest.mergeCommit);
My code:
type MergeCommit = { additions: number; deletions: number };
type isNotMerged = {
pullRequest: {
title: string;
merged: false;
mergedAt: null;
mergeCommit: null;
};
};
type isMerged = {
pullRequest: {
title: string;
merged: true;
mergedAt: string;
mergeCommit: MergeCommit;
};
};
type Pull = isMerged | isNotMerged;
async function main() {
try {
const response = await getContributions();
const data: Pull[] =
response.data.user.contributionsCollection.pullRequestContributions.nodes;
const commits: number = data.filter((pull) => pull.pullRequest.merged)
.length;
const mergeCommits: MergeCommit[] = data
.filter((pull: Pull) => pull.pullRequest.merged)
.map((pull: isMerged) => pull.pullRequest.mergeCommit);
const additions: NorU = mergeCommits
.map((com: MergeCommit) => com?.additions)
.reduce((accu: number, curr: number) => curr + accu);
const deletions: NorU = mergeCommits
.map((com: MergeCommit) => com?.deletions)
.reduce((accu: number, curr: number) => curr + accu);
}
}
In my mind, my logic clearly states that the types are assignable. Is there something I am missing about how the compiler can misinterpret .filter
/.map
arguements?
Any assistance is appreciated
I could share more code, but the type
declarations are quite exhaustive