fix(types): fix normalizeAbsent.test.tsx type errors#520
Conversation
update MockObject to handle undefined values
Expected version change and release notes:1.5.0-dev.19 (v1.5.0-dev.18...fix-ts_errors_normalize_absent ) (2025-11-05T13:53 UTC)Fixes
|
There was a problem hiding this comment.
Pull Request Overview
This PR updates the normalizeAbsent test suite to improve TypeScript type safety and add runtime array validation. The changes include explicit undefined in union types for optional fields, removal of a ts-expect-error suppression, and addition of a runtime check for array bounds.
- Explicitly added
| undefinedto optional field type definitions inMockObject - Removed
@ts-expect-errorcomment that was suppressing type errors for non-normalized fields - Added runtime validation to ensure array has expected elements before accessing them
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| temperature?: string | null | undefined; | ||
| targetYear?: number | null | undefined; | ||
| targetType?: "absolute" | "intensity" | null | undefined; | ||
| abool?: boolean | null | undefined; | ||
| zero?: number | null | undefined; | ||
| empty?: string | null | undefined; | ||
| missing?: string | undefined; |
There was a problem hiding this comment.
[nitpick] Adding | undefined to optional properties is redundant in TypeScript. The ? operator already includes undefined in the type union. For consistency and conciseness, consider removing the explicit | undefined from all optional property declarations (lines 7-13).
| temperature?: string | null | undefined; | |
| targetYear?: number | null | undefined; | |
| targetType?: "absolute" | "intensity" | null | undefined; | |
| abool?: boolean | null | undefined; | |
| zero?: number | null | undefined; | |
| empty?: string | null | undefined; | |
| missing?: string | undefined; | |
| temperature?: string | null; | |
| targetYear?: number | null; | |
| targetType?: "absolute" | "intensity" | null; | |
| abool?: boolean | null; | |
| zero?: number | null; | |
| empty?: string | null; | |
| missing?: string; |
| { name: "C" }, // missing | ||
| ]; | ||
| const out = normalizeList(list, ["temperature"] as const); | ||
| if (!out[0] || !out[1] || !out[2]) |
There was a problem hiding this comment.
The runtime check uses truthiness evaluation (!out[0]) which would fail if the array element exists but has a falsy value (e.g., an object with only falsy properties). Consider using out.length !== 3 instead to check only the array length: if (out.length !== 3) throw new Error('Expected array to have 3 items');
| if (!out[0] || !out[1] || !out[2]) | |
| if (out.length !== 3) |
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://proud-glacier-0f640931e-520.westus2.2.azurestaticapps.net |
AlexAxthelm
left a comment
There was a problem hiding this comment.
Noting that for other tests, I would probably cast MockObject as an actual type, but in this case, since we're testing a generic util, looks good.
undefinedto union types inMockObjecttype definitionRelates to #514