Help with Mongoose typings
See original GitHub issueI’m trying to create typings for Mongoose, but I don’t know if what I want to do is possible.
I want to start simple, so let’s ignore for now all the possible ways to define a model in Mongoose. My goal is to make something like this possible, where looking up a property that wasn’t defined in the schema will error in Flow.
import mongoose from 'mongoose';
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
},
});
const User = mongoose.model('User', UserSchema);
// And let's run a query
User.findOne({ email: 'example@example.com' }).exec()
.then((user) => {
// $ExpectError
user.lol
})
This is what I came up with, which doesn’t achieve my goal:
declare class Mongoose$Schema<Schema> {}
type Mongoose$Query<Schema> = {| [$Keys<Schema>]: any |}
type Mongoose$Projection<Schema> = { [$Keys<Schema>]: any }
declare type Mongoose$Result<Schema> = {| [$Keys<Schema>]: any |}
declare interface Mongoose$QueryInterface<Schema> {
exec(): Promise<Mongoose$Result<Schema> | void>
}
declare class Mongoose$Model<Schema> {
findOne(query: Mongoose$Query<Schema>, projection?: Mongoose$Projection<Schema>): Mongoose$QueryInterface<Schema>
}
Is what I wish to do even possible?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Using TypeScript with Mongoose
Mongoose introduced officially supported TypeScript bindings in v5.11.0. Mongoose's index.d.ts file supports a wide variety of syntaxes and strives to be ...
Read more >Working with Mongoose in TypeScript
Thus far, Mongoose works fairly well with TypeScript, but there's a lot of work to be done to make Mongoose's TypeScript bindings line...
Read more >Complete guide for Typescript with Mongoose for Node.js
To help developers with similar issues, I decided to write this blog regarding all the problems we run into and how to solve...
Read more >types/mongoose
Stub TypeScript definitions entry for mongoose, which provides its own types definitions. Latest version: 5.11.97, last published: 2 years ...
Read more >The Ultimate Guide to Typescript with Mongoose for Node.js
Repository: https://github.com/TomDoesTech/The-Ultimate-Guide-to- TypeScript -With-Mongoose0:00 Introduction1:15 What are we going to ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Who interested in mongoose type defs, please follow PR 1196
Yeah I think this issue can be closed.
Thank you all for the work!