feat: enhanced typing for CurrentUserChecker by introducing generics #1375
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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