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
feature: Introduce Generics to CurrentUserChecker for Enhanced Type Safety #1374
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 CurrentUserChecker to: export type CurrentUserChecker = <User = any>(action: Action) => Promise<User | null> | User | null;
This change will allow the function to return User | null or Promise<User | null>, where User is 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.
Description
Currently, the
CurrentUserChecker
function inrouting-controllers
uses a return type ofPromise<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:
CurrentUserChecker
to:export type CurrentUserChecker = <User = any>(action: Action) => Promise<User | null> | User | null;
User | null
orPromise<User | null>
, whereUser
is a generic type that can be defined by the developer.Alternative Solutions Considered:
any
.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.