Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhanced typing for CurrentUserChecker by introducing generics #1375

Merged

Conversation

angelxmoreno
Copy link
Contributor

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

  • 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

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
  • npm run prettier:check passes
  • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #1374

@attilaorosz
Copy link
Member

Thanks for the contribution!

@attilaorosz attilaorosz merged commit e8dfcf1 into typestack:develop Apr 15, 2024
5 checks passed
@angelxmoreno angelxmoreno deleted the feat/typed-CurrentUserChecker branch April 16, 2024 21:43
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

feature: Introduce Generics to CurrentUserChecker for Enhanced Type Safety
2 participants