Model's type parameter must be defined #12212
Closed
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.
In Typescript 4.8 and mongodb since at least 4.8.1,
mongodb.AnyBulkWriteOperation
requires its type argument to be defined. It forbidsnull
andundefined
. Previous versions of Typescript didn't check this properly for unconstrained type parameters (lone type parameters likeT
). Typescript 4.8, which is in beta and will release in a week or two, does.This PR adds a constraint to Model that requires its type parameter to be defined. It can no longer include
null
orundefined
. In particular, that meansModel<unknown>
is no longer allowed; you have to writeModel<{}>
instead, which is the same type excludingnull
andundefined
[1]. You can see this change in test/types/document.test.ts:179:This change ripples out to other types as well, most notably the type parameter for
Schema
, which now also disallowsnull
andundefined
.Alternative Fix
This is a pretty comprehensive PR. It should be possible to isolate the change to
Model.bulkWrite
by adding a conditional type:But this is unsafe since you can pass in writes that don't match Model's
T
, and because presumably AnyBulkWriteOperation really doesn't expectnull
orundefined
and might crash at runtime.I wouldn't recommend this smaller alternative except temporarily.
[1]
unknown
means "any value in Javascript".{}
means "any value except null or undefined".object
means "any non-primitive" (and does not allow null)Recommendation
I'd recommend testing with typescript@beta during the Typescript beta period or, even better, typescript@next.
Fixes #12213