diff --git a/src/entity-manager/EntityManager.ts b/src/entity-manager/EntityManager.ts index b51d1d601..d65d3877d 100644 --- a/src/entity-manager/EntityManager.ts +++ b/src/entity-manager/EntityManager.ts @@ -257,34 +257,34 @@ export class EntityManager { * Creates a new entity instance and copies all entity properties from this object into a new entity. * Note that it copies only properties that present in entity schema. */ - create( + create>( entityClass: EntityTarget, - plainObject?: DeepPartial, + plainObject?: EntityLike, ): Entity /** * Creates a new entities and copies all entity properties from given objects into their new entities. * Note that it copies only properties that present in entity schema. */ - create( + create>( entityClass: EntityTarget, - plainObjects?: DeepPartial[], + plainObjects?: EntityLike[], ): Entity[] /** * Creates a new entity instance or instances. * Can copy properties from the given object into new entities. */ - create( + create>( entityClass: EntityTarget, - plainObjectOrObjects?: DeepPartial | DeepPartial[], + plainObjectOrObjects?: EntityLike | EntityLike[], ): Entity | Entity[] { const metadata = this.connection.getMetadata(entityClass) if (!plainObjectOrObjects) return metadata.create(this.queryRunner) if (Array.isArray(plainObjectOrObjects)) - return (plainObjectOrObjects as DeepPartial[]).map( + return (plainObjectOrObjects as EntityLike[]).map( (plainEntityLike) => this.create(entityClass, plainEntityLike), ) diff --git a/src/repository/Repository.ts b/src/repository/Repository.ts index 4b8978d58..a9b8f87a3 100644 --- a/src/repository/Repository.ts +++ b/src/repository/Repository.ts @@ -126,7 +126,7 @@ export class Repository { | DeepPartial | DeepPartial[], ): Entity | Entity[] { - return this.manager.create( + return this.manager.create( this.metadata.target as any, plainEntityLikeOrPlainEntityLikes as any, ) diff --git a/test/github-issues/10569/contract/create-user-contract.ts b/test/github-issues/10569/contract/create-user-contract.ts new file mode 100644 index 000000000..beaa7476f --- /dev/null +++ b/test/github-issues/10569/contract/create-user-contract.ts @@ -0,0 +1,3 @@ +export type CreateUserContract = { + name: string +} diff --git a/test/github-issues/10569/entity/user.ts b/test/github-issues/10569/entity/user.ts new file mode 100644 index 000000000..d91bf2c0a --- /dev/null +++ b/test/github-issues/10569/entity/user.ts @@ -0,0 +1,10 @@ +import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" + +@Entity({ name: "user" }) +export class User { + @PrimaryGeneratedColumn("uuid") + id: string + + @Column("varchar") + name: string +} diff --git a/test/github-issues/10569/ussue-10569.ts b/test/github-issues/10569/ussue-10569.ts new file mode 100644 index 000000000..43dea6b41 --- /dev/null +++ b/test/github-issues/10569/ussue-10569.ts @@ -0,0 +1,42 @@ +import "reflect-metadata" + +import { expect } from "chai" +import { v4 } from "uuid" + +import { DataSource } from "../../../src/data-source/DataSource" +import { + closeTestingConnections, + createTestingConnections, + reloadTestingDatabases, +} from "../../utils/test-utils" +import { CreateUserContract } from "./contract/create-user-contract" +import { User } from "./entity/user" + +describe("github issues > #10569 Fix type inferencing of EntityManager#create", () => { + let dataSources: DataSource[] + before( + async () => + (dataSources = await createTestingConnections({ + entities: [__dirname + "/entity/*{.js,.ts}"], + schemaCreate: true, + dropSchema: true, + })), + ) + beforeEach(() => reloadTestingDatabases(dataSources)) + after(() => closeTestingConnections(dataSources)) + + it("should correctly inference entity type", () => + Promise.all( + dataSources.map(async (dataSource) => { + const createUserContract: CreateUserContract = { + name: "John Doe", + } + + const user = dataSource.manager.create(User, createUserContract) + + user.id = v4() + + expect(user.id).to.exist + }), + )) +})