feature: Introduce Generics to CurrentUserChecker for Enhanced Type Safety
angelxmoreno opened this issue · 2 comments
Description
Currently, the CurrentUserChecker function in routing-controllers uses a return type of Promise<any> | any, which doesn't utilize TypeScript's capabilities for strong typing. This can lead to potential type mismatches and less predictable code, especially in larger projects where strict type checking is crucial.
Proposed Solution
The CurrentUserChecker should be enhanced to support generic types, allowing developers to specify the expected user type more explicitly. This will improve code reliability and developer experience by leveraging TypeScript's type system more effectively.
Implementation:
- Modify the type definition of
CurrentUserCheckerto:export type CurrentUserChecker = <User = any>(action: Action) => Promise<User | null> | User | null; - This change will allow the function to return
User | nullorPromise<User | null>, whereUseris a generic type that can be defined by the developer.
Alternative Solutions Considered:
- An alternative would be to enforce a stricter non-nullable type by default, but this could introduce breaking changes for existing users who may rely on the flexibility of returning
any. - Another option could be to provide optional configuration settings in the library to enable strict typing, but this would complicate the library's configuration and usage.
The proposed solution strikes a balance between improving type safety and maintaining backward compatibility, thus providing a seamless upgrade path for current users while offering improved functionality.
I created a PR #1375
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.