You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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
Reacted by Sujan Tamang, Paul Pavlo Buidenkov, Ophélie GAUDIN, lukaszfedorko, Vedant Bothikar, Chris and kayibea
Activity
vvakame commentedon Dec 19, 2014
@horiuchi @psnider do you have something advise?
psnider commentedon Dec 19, 2014
I use this code:
horiuchi commentedon Dec 19, 2014
I use this code in my model module:
psnider commentedon Dec 20, 2014
I also found that I have used this:
because
gives this error:
psnider commentedon Dec 20, 2014
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:
Note that there is an issue with the particular compiler error that was being issued. See this issue: microsoft/TypeScript#722
davetayls commentedon Dec 21, 2014
Genius, of course that does make sense I'll give that a go a bit later. Cheers
davetayls commentedon Dec 22, 2014
sorry, that did compile but it outputs
type: typeof mongoose.Schema.Types.ObjectIdin the javascript.I tried
mongoose.Types.ObjectIdwhich works but it is actually different tomongoose.Schema.Types.ObjectIdso all the tests broke 😉psnider commentedon Dec 22, 2014
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:
Dev-Addict commentedon Nov 6, 2020
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.ObjectIdsisygoboom commentedon Nov 8, 2022
All you need is
mongoose.Types.ObjectId, the rest is a trollUpdates to Mongoose 4.5.x (DefinitelyTyped#10084)