Skip to content

Improve Type Safety Across the Codebase and Remove Remaining any Usage #541

Description

@Harxhit

Title

Replace Remaining any Types in auth.ts with Proper TypeScript Types.

Summary

Remove remaining usages of any in the authentication module and replace them with proper TypeScript interfaces, Fastify types, and request/user type definitions to improve type safety and maintainability.

Contexts

The authentication system still contains several any usages that bypass TypeScript's type checking. While the current implementation works, these areas can hide bugs and make refactoring more difficult.

Examples include:

request: any
reply: any
(request.user as any).id
(request.user as any)?.id

These should be replaced with proper interfaces and type declarations.

Tasks

  • Identify all remaining any usages in the auth.ts.
  • Replace middleware request: any with proper Fastify request types
  • Replace middleware reply: any with proper Fastify reply types
  • Create a typed interface for authenticated JWT payloads.
  • Extend Fastify request typings to support request.user
  • Remove type assertions such as (request.user as any)
  • Verify type safety with TypeScript checks
  • Update or add tests if required

Acceptance Criteria

  • No any types remain in the auth.ts file.
  • Authenticated user payload has a dedicated interface
  • request.user is properly typed through Fastify type augmentation
  • TypeScript passes without type assertions to any
  • Existing authentication flows continue to work
  • Tests pass successfully

Suggested Implementation

Create a dedicated JWT payload interface:

interface AuthenticatedUser {
  id: string;
  username: string;
}

Extend Fastify request typings:

declare module 'fastify' {
  interface FastifyRequest {
    user: AuthenticatedUser;
  }
}

Replace:

(request.user as any).id

with:

request.user.id

Replace:

request: any
reply: any

with:

request: FastifyRequest
reply: FastifyReply

Area

backend

Difficulty

Medium

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions