fix(types): improve Root shorthand method return types#3105
Open
mahmoodhamdi wants to merge 1 commit intohapijs:masterfrom
Open
fix(types): improve Root shorthand method return types#3105mahmoodhamdi wants to merge 1 commit intohapijs:masterfrom
mahmoodhamdi wants to merge 1 commit intohapijs:masterfrom
Conversation
…g declarations Change Root-level shorthand methods (allow, valid, invalid, required, optional, forbidden, etc.) to return AnySchema instead of Schema, since they delegate to this.any().method(). This enables proper method chaining with full type inference. Also add missing only() and strip() declarations to the Root interface, which are registered at runtime but were absent from the type definitions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The Root-level shorthand methods (
Joi.allow(),Joi.valid(),Joi.required(), etc.) currently declareSchemaas their return type. Since these methods delegate tothis.any()[method]()at runtime (source), they always return anAnySchema. Using the broaderSchemaunion type loses this specificity and breaks method chaining in TypeScript — e.g.Joi.allow('x').required()doesn't resolve toAnySchema.Changes
allow,valid,equal,invalid,disallow,not,required,exist,optional,forbidden,preferences,prefs,custom) fromSchematoAnySchemaonly()andstrip()declarations to the Root interface — these are registered at runtime but were absent from the type definitionsBefore
After
All tests pass:
npm test(1796 tests, 100% coverage, types, lint).