Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Types] Issues with official types #9856

Closed
stalniy opened this issue Jan 24, 2021 · 1 comment
Closed

[Types] Issues with official types #9856

stalniy opened this issue Jan 24, 2021 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@stalniy
Copy link

stalniy commented Jan 24, 2021

Do you want to request a feature or report a bug?
bug

What is the current behavior?

There are several issues with official types:

  1. Query is declared as an interface, so I cannot anymore do

    import { Query } from 'mongoose';
    
    const maybeQuery = {};
    console.log(maybeQuery instanceof Query) // 'Query' only refers to a type, but is being used as a value here

    But I can do this in pure js

    const { Query } = require('mongoose');
    
    const maybeQuery = {};
    console.log(maybeQuery instanceof Query) // false

    Fix: Query should be declared as class not as interface

  2. Query.where doesn't have signature for passing object in where but I can do this in js:

    // Post is a model
    Post.where({}) // TSError: Argument of type '{}' is not assignable to parameter of type 'string'.
    Post.where() // TSError: Expected 1-2 arguments, but got 0.
    Post.where({ $and: [{ isActive: true }, { id: 1 }] }) // TSError: Argument of type '{ $and: ({ isActive: boolean; } | { id: number; })[]; }' is not assignable to parameter of type 'string'

    all these are valid cases in pure js and perfectly worked with @types/mongoose

    Fix: add method overload for where

  3. No way to define custom Query methods added by a plugin (exist as a separate issue [types] Support custom Query Method types #9850)

If the current behavior is a bug, please provide the steps to reproduce.

  1. Create an empty npm folder
  2. Install latest mongoose and typescript npm i typescript mongoose
  3. Create index.ts and put inside every example from the description

What is the expected behavior?

Expected to not have TypeScript errors

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • mongoose@5.11.13
  • node@14.15.1
@vkarpov15 vkarpov15 added this to the 5.11.15 milestone Jan 28, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jan 28, 2021
@stalniy
Copy link
Author

stalniy commented Feb 1, 2021

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

2 participants