Conversation
…) and use expect-type for type assertions Fix #16012
There was a problem hiding this comment.
Pull request overview
This pull request addresses TypeScript type inference issues with subdocuments containing virtuals with setters (issue #16012) by allowing unknown keys in subdocuments while preserving autocomplete functionality. The PR also improves type assertions by adopting the expect-type library, which correctly handles the never type that the previous implementation did not.
Changes:
- Modified type casting logic to allow extra keys in subdocuments using
CreateObjectWithExtraKeys<T> = T & Record<string, unknown> - Refactored
ApplyBasicCreateCastingto check forTreatAsPrimitivesbefore general objects, ensuring correct type handling - Replaced type assertion declarations with expect-type library implementation for better type checking
- Added test case demonstrating the fix for gh16012 with virtual setters in subdocuments
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| types/models.d.ts | Adds CreateObjectWithExtraKeys type and updates ApplyBasicCreateCasting to allow unknown keys in subdocuments and nested objects while retaining type safety for known fields |
| test/types/util/assertions.ts | Converts ExpectType and ExpectAssignable from type declarations to runtime functions wrapping expect-type library for improved type assertion handling |
| test/types/create.test.ts | Adds gh16012 test demonstrating that virtuals can now be used in subdocument creation and that expect-type properly handles never type |
| package.json | Adds expect-type as a new dependency for improved type testing |
| "type": "commonjs", | ||
| "license": "MIT", | ||
| "dependencies": { | ||
| "expect-type": "^1.3.0", |
There was a problem hiding this comment.
The expect-type package should be moved to devDependencies rather than dependencies. This package is only used in TypeScript type tests (under test/types/) and is not needed for the runtime functionality of mongoose or for users of the mongoose package. Type test utilities are development dependencies, not runtime dependencies.
hasezoey
left a comment
There was a problem hiding this comment.
Aside from the dependency location, looks good to me
Fix #16012
Summary
Allows passing in extra keys to subdocuments while retaining autocomplete:
I decided to allow adding extra keys to subdocuments because adding the full virtuals to subdocuments would require more switches to
InferRawDocType,InferSchemaType, etc. and that would be unwieldy. Allowing unknown fields while retaining autocomplete seems like a reasonable middle ground.While working on this, I also found that our current
ExpectTypeassertions don't handleneverwell:neveris assignable to any type soExpectType<number>({} as never)unexpectedly succeeds:I wasn't able to come up with a reasonable workaround in ExpectType<> to correctly handle
never, so I'm pulling in expect-type npm package to help with that. I made currentassertions.tsa wrapper for expect-type for now to avoid massive diffs, but removing assertions.ts in favor of using expect-type directly is a candidate for future work.Examples