Coder Social home page Coder Social logo

Comments (13)

derekhawker avatar derekhawker commented on August 20, 2024 56

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.

szokodiakos avatar szokodiakos commented on August 20, 2024 8

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.

ndoulgeridis avatar ndoulgeridis commented on August 20, 2024 2

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.

singhsoldier13 avatar singhsoldier13 commented on August 20, 2024 1

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.

pgsfredda avatar pgsfredda commented on August 20, 2024

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.

anvlkv avatar anvlkv commented on August 20, 2024

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.

pgsfredda avatar pgsfredda commented on August 20, 2024

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.

anvlkv avatar anvlkv commented on August 20, 2024

@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.

pgsfredda avatar pgsfredda commented on August 20, 2024

@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.

anvlkv avatar anvlkv commented on August 20, 2024

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.

rolivegab avatar rolivegab commented on August 20, 2024

Are there any plans for Typegoose be compatible with target ES5?

from typegoose.

singhsoldier13 avatar singhsoldier13 commented on August 20, 2024

from typegoose.

Tom4U avatar Tom4U commented on August 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.