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

Can't get typegoose import and types in $push accepted by typescript at the same time #339

Closed
ezrafayet opened this issue Aug 3, 2020 · 6 comments
Labels
cant reproduce The current reproduce script does not work question Qustions about how to use stuff

Comments

@ezrafayet
Copy link

ezrafayet commented Aug 3, 2020

Hi all, first of all thanks for typegoose, I love it and I've been using it in production for the last 6 months (probably foolish but I really enjoy working with it).

My issue is probably just about me not knowing modules well enough (I just upgraded from typegoose from 6ish to 7.3.1).

Here is my model:

import { prop, getModelForClass } from "@typegoose/typegoose";
export { MyClass };
export { MyClassModel };

class MyClass {

  @prop({ items: String, default: [], })
  public thisIsAnArray?: string[];

}

const MyClassModel = getModelForClass(MyClass, {
  schemaOptions: { collection: 'myClass' },
});

Here is my code:

const myClass: MyClass = await MyClassModel.findOneAndUpdate({
  _id: objectId,
}, {
  $push: {
    thisIsAnArray: "hello there"
  }
}).lean();

My issue is that Webstorm throws an error and tsc won't compile.

for thisIsAnArray (in findOneAndUpdate) it tells me TS2769: No overload matches this call. The last overload gave the following error.  Type string is not assignable to type 'undefined'.

I played with tsconfig options a bit (changed commonjs modules to ES15), now the error is gone, but when I import
import {prop, getModelForClass} from '@typegoose/typegoose;'

it tells me TS2307: Cannot find module '@typegoose/typegoose' or its corresponding type declarations.

I tried to play with :

  • target
  • module
  • moduleResolution
  • esModuleInterop

There is always one or the other that has a problem, I can't get to have both working ...

(I tried importing typegoose from @typegoose/typegoose, then destructuring functions, but the same issue always appears)

  • typegoose 7.3.1
  • I have typescript 3.9.7
  • I have node 12.18.3

Edit:

I am using this trick for now ... (There are others but it will be the less painfull to refactor)

const getModelForClass = require('@typegoose/typegoose').getModelForClass;
const prop = require('@typegoose/typegoose').prop;
// import {prop, getModelForClass} from '@typegoose/typegoose';
@ezrafayet ezrafayet added the question Qustions about how to use stuff label Aug 3, 2020
@hasezoey
Copy link
Member

hasezoey commented Aug 3, 2020

(changed commonjs modules to ES15)

currently es6 modules(esm) are not supported by typegoose (look #214)

target

tsconfig option target needs to be es6 or higher for typegoose to work


cannot reproduce, code used:

// NodeJS: 14.7.0
// MongoDB: 4.2-bionic (Docker)
import { getModelForClass, prop } from "@typegoose/typegoose"; // @typegoose/typegoose@7.3.1
import * as mongoose from "mongoose"; // mongoose@5.9.26 @types/mongoose@5.7.34

class MyClass {
  @prop({ items: String, default: [], })
  public thisIsAnArray?: string[];
}

const MyClassModel = getModelForClass(MyClass, {
  schemaOptions: { collection: "myClass" },
});

(async () => {
  await mongoose.connect(`mongodb://localhost:27017/`, { useNewUrlParser: true, dbName: "verify339", useCreateIndex: true, useUnifiedTopology: true });

  const doc = new MyClassModel({});

  const myClass = await MyClassModel.findOneAndUpdate({
    _id: doc,
  }, {
    $push: {
      thisIsAnArray: "hello there" // has the correct typing and no error
    }
  }).lean();

  await mongoose.disconnect();
})();

do you have @types/mongoose installed?


PS: i edited your issue for better readability

@hasezoey hasezoey added the cant reproduce The current reproduce script does not work label Aug 3, 2020
@ezrafayet
Copy link
Author

ezrafayet commented Aug 3, 2020

Hi, thanks for the answer and trying to reproduce.

Yes I did install @types/mongoose (updated it too just in case).

Story is I temporary used "require" everywhere because I couldn't afford digging more right now into it. After my cleaning/refactoring, all of a sudden it started working like a charm ... I don't know exactly why but I also did two things in the meanwhile :

1/ I removed package-lock and re-built (for some other issues)

2/ Set this tsconfig :

"compilerOptions": {
    "target": "ES6", 
    "module": "commonjs",
    "outDir": "./build",                     
     "removeComments": true,                
    "strict": true,                          
    "noImplicitReturns": true,              
     "baseUrl": "./",                      
    "experimentalDecorators": true,      
    "emitDecoratorMetadata": true,    
    "forceConsistentCasingInFileNames": true,
  },

I have it all on a separate branch I'll check out of curiosity, and it's time for me to dig deeper into the different modules ...

Thanks a lot anyway for your help and library. I'm using it since it was hosted on your account, I really enjoy the simplicity it adds.

Our last year MVP and pivots did benefit a lot from it 👍

Edit: as you couldn't reproduce it (and me neither on a new project) and it was solved by deleting package.lock I closed it, I hope it's ok

@AhmedBHameed
Copy link

@adzaria Did you figure out what is the cause of this typescript error?
I have the same error :(

@loicnestler
Copy link

Any updates on this? Having the same issue.

@hasezoey
Copy link
Member

@loicnestler no updates, could you list your used versions? typegoose, mongoose, typescript, @types/mongoose

@loicnestler
Copy link

@hasezoey actually it was my bad. :oops:
I just forgot to make the typescript type in my model an array.
So no issue for me!

Thank you for quick response tho!

Still, in case anyone else has this issue:
"@types/mongoose": "^5.10.3"
"mongoose": "5.10.18"
"@typegoose/typegoose": "^7.6.0"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cant reproduce The current reproduce script does not work question Qustions about how to use stuff
Projects
None yet
Development

No branches or pull requests

4 participants