Skip to content

Commit 99d8249

Browse files
authored
fix: type inferencing of EntityManager#create (#10569)
* fix: type inferencing of EntityManager#create * test: type inferencing of EntityManager#create (#10569)
1 parent 0cab0dd commit 99d8249

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

src/entity-manager/EntityManager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,34 +257,34 @@ export class EntityManager {
257257
* Creates a new entity instance and copies all entity properties from this object into a new entity.
258258
* Note that it copies only properties that present in entity schema.
259259
*/
260-
create<Entity>(
260+
create<Entity, EntityLike extends DeepPartial<Entity>>(
261261
entityClass: EntityTarget<Entity>,
262-
plainObject?: DeepPartial<Entity>,
262+
plainObject?: EntityLike,
263263
): Entity
264264

265265
/**
266266
* Creates a new entities and copies all entity properties from given objects into their new entities.
267267
* Note that it copies only properties that present in entity schema.
268268
*/
269-
create<Entity>(
269+
create<Entity, EntityLike extends DeepPartial<Entity>>(
270270
entityClass: EntityTarget<Entity>,
271-
plainObjects?: DeepPartial<Entity>[],
271+
plainObjects?: EntityLike[],
272272
): Entity[]
273273

274274
/**
275275
* Creates a new entity instance or instances.
276276
* Can copy properties from the given object into new entities.
277277
*/
278-
create<Entity>(
278+
create<Entity, EntityLike extends DeepPartial<Entity>>(
279279
entityClass: EntityTarget<Entity>,
280-
plainObjectOrObjects?: DeepPartial<Entity> | DeepPartial<Entity>[],
280+
plainObjectOrObjects?: EntityLike | EntityLike[],
281281
): Entity | Entity[] {
282282
const metadata = this.connection.getMetadata(entityClass)
283283

284284
if (!plainObjectOrObjects) return metadata.create(this.queryRunner)
285285

286286
if (Array.isArray(plainObjectOrObjects))
287-
return (plainObjectOrObjects as DeepPartial<Entity>[]).map(
287+
return (plainObjectOrObjects as EntityLike[]).map(
288288
(plainEntityLike) => this.create(entityClass, plainEntityLike),
289289
)
290290

src/repository/Repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class Repository<Entity extends ObjectLiteral> {
126126
| DeepPartial<Entity>
127127
| DeepPartial<Entity>[],
128128
): Entity | Entity[] {
129-
return this.manager.create<any>(
129+
return this.manager.create(
130130
this.metadata.target as any,
131131
plainEntityLikeOrPlainEntityLikes as any,
132132
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type CreateUserContract = {
2+
name: string
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity({ name: "user" })
4+
export class User {
5+
@PrimaryGeneratedColumn("uuid")
6+
id: string
7+
8+
@Column("varchar")
9+
name: string
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import "reflect-metadata"
2+
3+
import { expect } from "chai"
4+
import { v4 } from "uuid"
5+
6+
import { DataSource } from "../../../src/data-source/DataSource"
7+
import {
8+
closeTestingConnections,
9+
createTestingConnections,
10+
reloadTestingDatabases,
11+
} from "../../utils/test-utils"
12+
import { CreateUserContract } from "./contract/create-user-contract"
13+
import { User } from "./entity/user"
14+
15+
describe("github issues > #10569 Fix type inferencing of EntityManager#create", () => {
16+
let dataSources: DataSource[]
17+
before(
18+
async () =>
19+
(dataSources = await createTestingConnections({
20+
entities: [__dirname + "/entity/*{.js,.ts}"],
21+
schemaCreate: true,
22+
dropSchema: true,
23+
})),
24+
)
25+
beforeEach(() => reloadTestingDatabases(dataSources))
26+
after(() => closeTestingConnections(dataSources))
27+
28+
it("should correctly inference entity type", () =>
29+
Promise.all(
30+
dataSources.map(async (dataSource) => {
31+
const createUserContract: CreateUserContract = {
32+
name: "John Doe",
33+
}
34+
35+
const user = dataSource.manager.create(User, createUserContract)
36+
37+
user.id = v4()
38+
39+
expect(user.id).to.exist
40+
}),
41+
))
42+
})

0 commit comments

Comments
 (0)