You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I create a class and extend BaseEntity I get the active record style of interaction as expected. However the methods on the base class only return type BaseEntity. For example, here's a simple active record entity:
Now if I save a new entity it will save it as expected:
awaitProject.save({comment: "hello, typeorm"});
However, if I then try to get the returned entity and read it's ID it will complain:
constnewProject=awaitProject.create({comment: "hello, typeorm"});console.log(newProject.id);// does not work
Here we get Property 'id' does not exist on type 'BaseEntity'. I have to manually cast the generic save method to rectify this, which kind of defeats the point of active record simplicity a little bit.
constnewProject=awaitProject.create<Project>({comment: "hello, typeorm"});console.log(newProject.id);// this works
Expected Behavior
I would expect the save method in this case to return an instance of Project.
My Environment
Dependency
Version
Operating System
Node.js version
16.14.2
Typescript version
4.7.4
TypeORM version
0.3.7
Relevant Database Driver(s)
DB Type
Reproducible
aurora-mysql
no
aurora-postgres
no
better-sqlite3
no
cockroachdb
no
cordova
no
expo
no
mongodb
no
mysql
no
nativescript
no
oracle
no
postgres
yes
react-native
no
sap
no
spanner
no
sqlite
no
sqlite-abstract
no
sqljs
no
sqlserver
no
Are you willing to resolve this issue by submitting a Pull Request?
✖️ Yes, I have the time, and I know how to start.
✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
✖️ No, I don’t have the time, but I can support (using donations) development.
✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
The text was updated successfully, but these errors were encountered:
Maybe it's not easy to get the type from static class methods without a manually specified generic but at the very least I'd love to be able to specify the type on the class itself.
classProjectextendsBaseEntity<Project>{/* ... */}
At least then we'd only have to specify it once instead of every time you use a static method on the class.
It's returning the wrong type, it should return the entity's class.
Maybe it's not easy to get the type from static class methods without a manually specified generic but at the very least I'd love to be able to specify the type on the class itself.
I'm pretty sure It was working ok in version 0.2.x, the code in the screenshot above wasn't showing any errors at least.
This is a bit of a bummer because we were hoping to use await SomeEntity.create(entityProperties).save() directly in test setup logic but we're having to fall back to this instead to get proper typings:
Issue Description
If I create a class and extend
BaseEntity
I get the active record style of interaction as expected. However the methods on the base class only return typeBaseEntity
. For example, here's a simple active record entity:Now if I save a new entity it will save it as expected:
However, if I then try to get the returned entity and read it's ID it will complain:
Here we get
Property 'id' does not exist on type 'BaseEntity'
. I have to manually cast the genericsave
method to rectify this, which kind of defeats the point of active record simplicity a little bit.Expected Behavior
I would expect the
save
method in this case to return an instance ofProject
.My Environment
Relevant Database Driver(s)
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
spanner
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: