typestack / routing-controllers

Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework.
MIT License
4.36k stars 392 forks source link

feat: enhanced typing for CurrentUserChecker by introducing generics #1375

Closed angelxmoreno closed 2 months ago

angelxmoreno commented 2 months ago

Overview

This PR introduces a change to the CurrentUserChecker type definition to support generics, allowing for more precise type inference and usage in projects utilizing routing-controllers.

Changes

Motivation

Previously, CurrentUserChecker was typed to return any or Promise<any>, which did not leverage TypeScript's full capabilities for type safety and led to less precise type inference in consuming code. By introducing generics, developers can now specify the expected user object type, leading to cleaner and more maintainable codebases.

Compatibility

The default generic type is any, which maintains the current behavior for existing codebases using routing-controllers. This update is fully backwards compatible and does not require any changes to existing implementations unless the consuming codebase opts into using generics for more specific typing.

Example

With the updated CurrentUserChecker, developers can now define the expected return type explicitly:

const currentUserChecker: CurrentUserChecker<MyUserType> = (action: Action) => {
  // Implementation that returns MyUserType or null
};

This ensures that the type of the user object is known and checked at compile time, reducing runtime errors and improving the developer experience.

Checklist

Fixes

fixes #1374

attilaorosz commented 2 months ago

Thanks for the contribution!

github-actions[bot] commented 1 month ago

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.