question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

field with Types.ObjectId has typescript error in Schema

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.5.4

Node.js version

16.14.2

MongoDB version

doesn’t matter

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.5.1

Issue

import { Schema, Types } from 'mongoose';

interface Foo {
  _id: Types.ObjectId,
  bar: Types.ObjectId,
}

const FooSchema = new Schema<Foo>({
  _id: { type: Types.ObjectId },
  bar: { type: Types.ObjectId },
});

property _id and bar has typescript error:

Type '{ type: typeof Types.ObjectId; }' is not assignable to type 'SchemaDefinitionProperty<ObjectId> | undefined'.
  Types of property 'type' are incompatible.
    Type 'typeof ObjectId' is not assignable to type 'typeof Mixed | ObjectIdSchemaDefinition | undefined'.ts(2322)

But I cannot figure out why, and don’t know how to solve it.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Sep 14, 2022

sorry that i didnt see that before, but yes it seems like mongoose currently does not translate mongoose.Schema.Types.* to mongoose.Types.* in types and you have to use the latter for the types, but the former for the Schema itself (in new Schema(here))

1reaction
hasezoeycommented, Sep 14, 2022

I tried few things with HydratedDocument and LeanDocument but they don’t change the Schema.Types.ObjectId into Types.ObjectId. smiling_face_with_tear

the proper type should be type YourDocument = mongoose.Document<IdType> & YourSchema, also see Mongoose Documentation: Typescript

see typegoose’s implementation for a full (generic) example: https://github.com/typegoose/typegoose/blob/06f710df2d47a7501a496dc877dce955e047fe2b/src/types.ts#L14-L18

// the following is from typegoose, modified without typegoose-specific things
export type DocumentType<T, QueryHelpers = BeAnObject> = (T extends { _id: unknown }
  ? mongoose.Document<T['_id'], QueryHelpers, T>
  : mongoose.Document<any, QueryHelpers, T>) &
  T;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose TypeScript ObjectId casting - Type 'string' is not ...
When I try to cast string[] to Schema.Types.ObjectId[] array, the error goes away but this cannot be the solution, since it crashes at...
Read more >
Populate with TypeScript - Mongoose
Mongoose's TypeScript bindings add a generic parameter Paths to the populate() : import { Schema, model, Document, Types } from 'mongoose'; ...
Read more >
Error & Warning Details - typegoose
This Error may get thrown when a invalid Type is used with the option enum . Currently Valid Types for enum are String...
Read more >
TypeScript — Node.js - MongoDB
The type error raised by the preceding code snippet is as follows: ... How you specify the _id field in type parameters passed...
Read more >
Express Tutorial Part 3: Using a Database (with Mongoose)
The documents will contain the fields/schema types defined in the model ... If the query is successful, the error parameter will be null, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found