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.

Help with Mongoose typings

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nodkzcommented, Aug 30, 2017

Who interested in mongoose type defs, please follow PR 1196

0reactions
albertorestifocommented, Sep 29, 2017

Yeah I think this issue can be closed.

Thank you all for the work!

Read more comments on GitHub >

github_iconTop 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 >

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