fix(types): correct Model.validate() return type to Promise<TRawDocType>#16340
Merged
vkarpov15 merged 1 commit intoJun 23, 2026
Merged
Conversation
Model.validate() casts and validates the input and returns the casted-and-validated copy (see lib/model.js: returns obj after this.castObject(obj)), as the JSDoc already documents (@return casted and validated copy of obj). The TypeScript declarations incorrectly typed all overloads as Promise<void>, so the resolved value could not be used. Mirror castObject()'s TRawDocType return type and add type tests. Fixes Automattic#16338
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.
Summary
Model.validate()is typed to resolve tovoid, but at runtime it returns the casted-and-validated copy of the input object.In
lib/model.js,Model.validate()doesobj = this.castObject(obj)and ends withreturn obj;. The JSDoc on the implementation already documents this:However, the TypeScript declarations in
types/models.d.tstyped every overload asPromise<void>, so consumers cannot use the resolved value:This PR changes the return type to
Promise<TRawDocType>, mirroring the adjacentcastObject(...): TRawDocTypedeclaration (validate()returns exactly whatcastObject()produced). The existingModel['~standard'].validate()adapter already assumesvalidateresolves to the data (it wraps the result as{ value }), and its type test assertsresult.valueis the raw doc type — consistent with this change.Fixes #16338
Examples
Added type tests in
test/types/models.test.ts(modelValidateReturnsCastedObject):These fail before the fix (
Property 'age' does not exist on type 'void') and pass after. Fullnpm run test:typessuite passes (39 files, 978 assertions),eslintclean.