Comments (13)
I had the same error as the initial post It was caused by a tsconfig.json setting (target: "es5"). Setting target to "es6" fixed the errors. I guess es5 functions are not a direct replacement for class inheritance in this case.
Hope this catches the eye of anyone with a similar issue. On my end it was mostly just a copy+paste problem since I use typescript with web projects usually and using es6 there isn't really great for compatibility.
from typegoose.
Thanks for trying to resolve the issue @pgsfredda and @derekhawker .
So if I undestand this right: Typegoose will not work with TypeScript projects targeted to "es5" right, only es6/es2015 ? @anvlkv can you try out your code with target set to es6 and post us the results?
If that is the case I'll have to add this note to the documentation (or try to find a way to make Typegoose es5 target compatible)
from typegoose.
Sorry recovering this issue but is Typegoose now ES5 compatible? I still get this error when I use es5 and I need es5 as a target.
from typegoose.
I am facing this issue as well and I cannot use ES6 as target in my project. Is there any way I can make this work with ES5?
from typegoose.
Hi @anvlkv.
Try in this manner:
export const ItemModel = new Item().getModelForClass(Item);
export class ItemsService {
private itemModel: InstanceType<Item>;
constructor(
) {
this.itemModel = new ItemModel({ /* here you can put some field to inizialize */ });
}
}
from typegoose.
Hi @pgsfredda,
Thanks for looking into my problem
I tried your suggestion, it just shifted the error to where I export const ItemModel
I've got this repo with reproduction
from typegoose.
Hi @anvlkv
I have understand your problem. The correct source is:
import { prop, Typegoose } from 'typegoose';
export class Item extends Typegoose {
@prop()
name: string;
@prop()
description: string;
}
export const ItemModel = new Item().getModelForClass(Item); //Here is the correction. The type of ItemModel is inferred
let trial = new ItemModel({name:'some', description:'some'});
console.log(trial);
// prints:
// { name: 'some',
// description: 'some',
// _id: 59e65dcf24b047492f48e13f }
By the way, no monogoose Model is needed. ItemModel represents the schema and the collection of data with the two fields 'name' and 'description' and all the statics, the methods and the plugins.
If you want to declare e function param you can use:
function( item: InstanceType<Item>) { ... }
from typegoose.
@pgsfredda Hmmm,
Still got the same issue.
If I'm not declaring type like export const ItemModel: Model<InstanceType<Item>>
I get typescript error TS4023:
Error:(11, 14) TS4023:Exported variable 'ItemModel' has or is using name 'Document' from external module "mongoose" but cannot be named.
Error:(11, 14) TS4023:Exported variable 'ItemModel' has or is using name 'Model' from external module "mongoose" but cannot be named.
According to this microsoft/TypeScript#5711 solution is to declare it.
Since in this example I have it all in one file I can even ignore export and just do const ItemModel = new Item().getModelForClass(Item)
as you suggested.
from typegoose.
@anvlkv
I don't undestand why.
My source example works correctly. I use that pattern always and works fine.
Of course, you have to use 'export' instruction to use ItemModel in other files.
The pattern for the 'model file' is:
import { Typegoose, prop } from 'typegoose';
export class Item exrends Typegoose {
@prop() field: simple type of field (string, number, ...)
...
};
export const ItemModel = new Item().getModelForClass(Item);
Then you can use ItemModel to active find, update and create methods in the other files.
Check also your grunt and tsconfig files for the options about decorations (see the README.md file).
from typegoose.
Cool! Thank you guys! That totally helped.
I don't have any need to use other targets.
update: or may be I actually do))
from typegoose.
Are there any plans for Typegoose be compatible with target ES5?
from typegoose.
from typegoose.
It's a real issue, as Angular's test (spec) files cannot be run properly, if I try to use a model that is based on Typegoose. Will you take over ES5 compatibility from @singhsoldier13 in future??
from typegoose.
Related Issues (20)
- Please use the new Repository to open new issues HOT 4
- Pre hook supplied function error HOT 56
- TypeError: Class constructor Typegoose cannot be invoked without 'new' HOT 1
- [Request] Setting Collection Name and other Schema options via annotation and reflections
- [Question] Import ObjectId from Typegoose? HOT 4
- Please archive this repo
- Is there any way to achieve schema inheritance with typegoose? HOT 2
- [Question] Type of User.x property is not a y HOT 1
- Is it possible to use getModelForClass with generics? HOT 1
- [Question] HOT 1
- my populate field cant be queried out HOT 3
- [Question] HOT 1
- [Question] Problems reusing the same schema for both collection and subdocument HOT 1
- [Question] Can we add custom error messages when @prop options fail? HOT 2
- [Question] Are transactions supported for deleteMany? HOT 1
- [Question] Possible to have Query type support? HOT 1
- bi-directional virtuals population issue HOT 1
- Cannot use same model on two connections. HOT 2
- [Question] Is there a `required if` feature on model classes? HOT 2
- Both updatedBy and CreatedBy are getting updated after update operation on model object HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typegoose.