From a11b4709a7680d6eee7a48660fae7cbe0eca8dcb Mon Sep 17 00:00:00 2001 From: Janpeter Visser Date: Sat, 2 May 2026 20:21:30 +0200 Subject: [PATCH] fix(insights): narrow Sprint.completed_at to Date in velocity.ts (#56) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vercel build was failing with TS18047 — `sprint.completed_at` is possibly `null`. The earlier `.filter(s => s.completed_at != null)` runtime-filtered the nulls out but did NOT narrow the element type; TypeScript still saw `Date | null` on the result. Add a user-defined type guard `(s): s is SprintWithCompletedAt =>` so the narrowed array carries `completed_at: Date`. No runtime change. Co-authored-by: Claude Opus 4.7 (1M context) --- lib/insights/velocity.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/insights/velocity.ts b/lib/insights/velocity.ts index 7cf123e..c01c45e 100644 --- a/lib/insights/velocity.ts +++ b/lib/insights/velocity.ts @@ -33,7 +33,12 @@ export async function getVelocity(userId: string, sprintsBack = 5): Promise s.completed_at != null).reverse() + // Type-guard so the narrowed array carries `completed_at: Date` (not Date | null). + // A `.filter(s => s.completed_at != null)` alone does NOT narrow the element type. + type SprintWithCompletedAt = (typeof sprints)[number] & { completed_at: Date } + const chronological = [...sprints] + .filter((s): s is SprintWithCompletedAt => s.completed_at != null) + .reverse() const result: VelocitySprint[] = chronological.map(sprint => ({ sprintId: sprint.id,