Model.exists
result _id
is the document's type
#12094
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Prerequisites
Mongoose version
6.4.3
Node.js version
18.4.0
MongoDB server version
5.0
Description
Using Model.exists to check if a document exists with the specified query is meant to return the Document's
_id
if it exists and null otherwise.But I get a TS error when accessing the
_id
because it's not the expected value type.When I read the types to understand the error in these lines,
mongoose/types/models.d.ts
Lines 308 to 313 in db1ba6e
The first parameter in the
Document
class is expected to be_id
's type. But theT
variable that's passed is the Schema's resolved type.mongoose/types/document.d.ts
Lines 13 to 23 in db1ba6e
From the documatation,
So the
RequireOnlyTypedId
type could be better to use as theModel.exists
return type because the_id
property always exists and the returned document is always a POJO unless it's not found thennull
.mongoose/types/index.d.ts
Lines 117 to 119 in db1ba6e
Then the method definition would be something like,
Steps to Reproduce
Expected Behavior
For the
_id
property to be the Document's_id
type not the entire document type.The text was updated successfully, but these errors were encountered: