Improve Type Safety in cards.ts and Remove Remaining any Usage#579
Improve Type Safety in cards.ts and Remove Remaining any Usage#579Facelessism wants to merge 1 commit into
Conversation
Signed-off-by: lost_fanatic <bighna2005@gmail.com>
|
@Facelessism is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @Facelessism, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — All Checks PassedBackend — PASS
Mobile — SKIP
Web — SKIP
Last updated: |
|
Hey @Harxhit |
Harxhit
left a comment
There was a problem hiding this comment.
Checked this out and tested locally. This is a clean, well-scoped refactor — every any in cards.ts is genuinely removed (not relocated or suppressed), and the new typings are all backed by augmentations that already exist in the repo.
Verified ✅
- Typecheck passes —
npm run typecheck(tsc --noEmit) completes with no errors. - Lint passes —
npx eslint src/routes/cards.tsis clean. - Tests pass —
npx vitest run src/__tests__/cards.test.ts→ 21/21 green. The suite exercises the refactored error branches:OWNERSHIP→ 403 (create & update) andLAST_CARD→ 400 (delete), confirming the switch fromcatch (error: any)to thehasErrorCodeguard is behavior-preserving. request.user.idis sound: typed asAuthenticatedUservia the@fastify/jwtaugmentation insrc/types/fastify.d.ts, so the(request.user as { id: string })casts were safely removable.app.authenticate/server.authenticateare properly typed onFastifyInstance, so dropping(app as any)andrequest.server as anyis correct.hasErrorCodeguard is correct — the service throws viaObject.assign(new Error(...), { code: '...' }), socodeis an own string property;'code' in error && typeof error.code === 'string'narrows correctly. It's actually slightly stricter than the olderror?.code === '...'— an improvement.- No unsafe casts introduced — the
anycasts are replaced by a real runtime type guard.
Suggestions
- (nit / optional) In the
preHandler,request.serveris typed asFastifyInstanceon whichauthenticateis non-optional, so thetypeof server?.authenticate === 'function'runtime guard is somewhat redundant — though a reasonable defensive check that matches the existing pattern. Fine to leave. - (nit / follow-up, out of scope) The same
as anyauth pattern still exists in sibling route files (profiles.ts,event.ts,nfc.ts,team.ts,follow.ts,analytics.ts). Worth a follow-up issue to apply the same treatment now that the typings are proven to support it.
Nothing blocking. Behavior is unchanged and verified by the existing card tests.
Reviewed locally — ran npm run typecheck, npx eslint src/routes/cards.ts, and npx vitest run src/__tests__/cards.test.ts against the branch.
Summary
This PR improves type safety in
cards.tsby removing the remaining usages ofanyand replacing them with existing Fastify/JWT typings that's already available in the codebase. Auth-related type assertions have been eliminated, error handling has been made safer through an explicit type narrowing and a shared Fastify/JWT definitions are now reused where applicable.Closes #547
Type of Change
What Changed
anyusages fromsrc/routes/cards.tsrequest.server as anyand(app as any).authenticatewith properly typed fastify decorator accessrequest.userand auth related logiccatch (error: any)with safer error handling using explicit type narrowingHow to Test
apps/backendnpm run typecheckand verify it completes without any errorsChecklist
pnpm -r run lintpasses).pnpm -r run typecheck).pnpm -r run test).console.logor debug statements left in the code.Screenshots / Recordings
Additional Context