fix(types): enhance reusability of pres #4470
Merged
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.
Support for pre handlers whose type definition is only a subset of the actual route type definition.
When trying to reuse pre handlers in a typescript environment like the following
The type definition of the pre handler does not have any possible valid value, hence needing to drop type checking using the
any
keyword in order for typescript not to throw an error.This happens because the definition of
Lifecycle.Method
expects the request type to equal to the type provided in theMethod
generic, but in this scenario there're 2 distinct request types.The changes in this PR update the
Method
type definition to require the request type to extend the type provided in the generic instead of checking for it to be equal.This change allows to resolve the above scenario with type definitions like the following:
As all route definitions contain a
preParam: number
param, they're successfully extending the Method's generic, passing type checking