Skip to content

mongoose ObjectId #3345

Closed
Closed

Description

@davetayls

I am defining a Schema which includes a type ObjectId. In my JavaScript I would use mongoose.Schema.Types.ObjectId but when I convert it to Typescript I get the following error:

error TS2305: Module '"mongoose"' has no exported member 'Schema'.

This is odd because there is definitely an export class Schema in the typescript definition with a static Types property.

here is my Typescript code

import mongoose = require('mongoose');

export interface IManagerSchema extends mongoose.Document {
  user: mongoose.Schema.ObjectId;
}

export var ManagerSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: true
  }
});

I've been trying to work this out for a couple of days now so any help would be really appreciated

Activity

  1. vvakame commented on Dec 19, 2014

    @vvakame
    Member

    @horiuchi @psnider do you have something advise?

  2. psnider commented on Dec 19, 2014

    @psnider
    Contributor

    I use this code:

    import mongoose                         = require('mongoose');
    var id = new mongoose.Types.ObjectId;
    
  3. horiuchi commented on Dec 19, 2014

    @horiuchi
    Member

    I use this code in my model module:

    import mongoose = require('mongoose');
    
    export class ObjectId implements mongoose.Types.ObjectId {}
  4. psnider commented on Dec 20, 2014

    @psnider
    Contributor

    I also found that I have used this:

    import mongoose                         = require('mongoose');
    var ObjectId                            = mongoose.Schema.Types.ObjectId;
    

    because

    import mongoose                         = require('mongoose');
    import ObjectId                            = mongoose.Schema.Types.ObjectId;
    

    gives this error:

    test.ts(22,1): error TS2305: Module '"mongoose"' has no exported member 'Schema'.
    
  5. psnider commented on Dec 20, 2014

    @psnider
    Contributor

    I looked into this a little further and discovered how to get your code to compile. Note the introduction of the keyword typeof in two places:

    /// <reference path='mongoose.d.ts' />
    import mongoose = require('mongoose');
    
    export interface IManagerSchema extends mongoose.Document {
      user: typeof mongoose.Schema.Types.ObjectId;
    }
    
    export var ManagerSchema = new mongoose.Schema({
      user: {
        type: typeof mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
      }
    });
    

    Note that there is an issue with the particular compiler error that was being issued. See this issue: microsoft/TypeScript#722

  6. davetayls commented on Dec 21, 2014

    @davetayls
    Author

    Genius, of course that does make sense I'll give that a go a bit later. Cheers

  7. davetayls commented on Dec 22, 2014

    @davetayls
    Author

    sorry, that did compile but it outputs type: typeof mongoose.Schema.Types.ObjectId in the javascript.

    I tried mongoose.Types.ObjectId which works but it is actually different to mongoose.Schema.Types.ObjectId so all the tests broke 😉

  8. psnider commented on Dec 22, 2014

    @psnider
    Contributor

    It appears that typeof is treated differently in different contexts: in some it is a TypeScript keyword, and in others it is treated as a javascript keyword. I haven't yet figured out when or why it differs.

    But I played with your code a bit more and think I may have a solution, by using the Types module instead of the reference from within Schemas. Again, I don't know enough about TypeScript namespaces to answer why. Perhaps this will work for you:

    /// <reference path='mongoose.d.ts' />
    import mongoose = require('mongoose');
    import Schema = mongoose.Schema;
    import Types = mongoose.Types;
    import ObjectId = Types.ObjectId;
    
    export interface IManagerSchema extends mongoose.Document {
      user: ObjectId;
    }
    
    export var ManagerSchema = new mongoose.Schema({
      user: {
        type: ObjectId,
        ref: 'User',
        required: true
      }
    });
    
    
  9. Dev-Addict commented on Nov 6, 2020

    @Dev-Addict

    I found out the ObjectId type is now defined under Types only when you are using package @types/mongoose
    so you should use it like this
    import mongoose from 'mongoose';
    const ObjectId = mognoose.Types.ObjectId

  10. sisygoboom commented on Nov 8, 2022

    @sisygoboom

    I use this code in my model module:

    import mongoose = require('mongoose');
    
    export class ObjectId implements mongoose.Types.ObjectId {}

    All you need is mongoose.Types.ObjectId, the rest is a troll

  11. added a commit that references this issue on Jan 22, 2026
    c6757a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions