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
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
The CurrentUserChecker type definition has been modified to accept a generic type parameter User. This parameter defaults to any, ensuring backward compatibility.
The return type has been updated to include User | null in addition to Promise<User | null>. This change enhances type safety by allowing the function to explicitly specify a nullable user object, which aligns better with TypeScript practices where explicit types are preferable for nullability.
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
[x] the pull request title describes what this PR does (not a vague title like Update index.md)
[x] the pull request targets the default branch of the repository (develop)
[x] the code follows the established code style of the repository
[x] npm run prettier:check passes
[x] npm run lint:check passes
[ ] tests are added for the changes I made (if any source code was modified)
[ ] documentation added or updated
[x] I have run the project locally and verified that there are no errors
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.
Overview
This PR introduces a change to the
CurrentUserChecker
type definition to support generics, allowing for more precise type inference and usage in projects utilizingrouting-controllers
.Changes
CurrentUserChecker
type definition has been modified to accept a generic type parameterUser
. This parameter defaults toany
, ensuring backward compatibility.User | null
in addition toPromise<User | null>
. This change enhances type safety by allowing the function to explicitly specify a nullable user object, which aligns better with TypeScript practices where explicit types are preferable for nullability.Motivation
Previously,
CurrentUserChecker
was typed to returnany
orPromise<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 usingrouting-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: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
Update index.md
)develop
)npm run prettier:check
passesnpm run lint:check
passesFixes
fixes #1374