fix(insights): narrow Sprint.completed_at to Date in velocity.ts (#56)

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) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-02 20:21:30 +02:00 committed by GitHub
parent b2427fd07b
commit a11b4709a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,7 +33,12 @@ export async function getVelocity(userId: string, sprintsBack = 5): Promise<Velo
})
// Reverse to chronological order (oldest first, for x-axis)
const chronological = [...sprints].filter(s => 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,