Skip to content

Commit

Permalink
fix: improve DeepPartial type (#8187)
Browse files Browse the repository at this point in the history
* test: DeepPartial with generics

* fix: resolve DeepPartial generic not matching valid input

* fix: resolve TS errors due to DeepPartial change

* deep partial type simplification

Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
  • Loading branch information
MatthiasKunnen and pleerock committed Feb 16, 2022
1 parent a11c50d commit b93416d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
9 changes: 3 additions & 6 deletions src/common/DeepPartial.ts
@@ -1,9 +1,6 @@
/**
* Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties.
*/
export type DeepPartial<T> = {
[P in keyof T]?:
T[P] extends Array<infer U> ? Array<DeepPartial<U>> :
T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> :
DeepPartial<T[P]> | T[P]
};
export type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : T;
2 changes: 1 addition & 1 deletion src/entity-manager/EntityManager.ts
Expand Up @@ -247,7 +247,7 @@ export class EntityManager {
return metadata.create(this.queryRunner);

if (Array.isArray(plainObjectOrObjects))
return plainObjectOrObjects.map(plainEntityLike => this.create(entityClass as any, plainEntityLike));
return (plainObjectOrObjects as DeepPartial<Entity>[]).map(plainEntityLike => this.create(entityClass, plainEntityLike));

const mergeIntoEntity = metadata.create(this.queryRunner);
this.plainObjectToEntityTransformer.transform(mergeIntoEntity, plainObjectOrObjects, metadata, true);
Expand Down
12 changes: 12 additions & 0 deletions test/github-issues/2904/entity/Comment.ts
@@ -0,0 +1,12 @@
import {
Column,
CreateDateColumn,
} from "../../../../src";

export class Comment {
@CreateDateColumn()
createdAt: Date;

@Column()
savedBy: string;
}
14 changes: 14 additions & 0 deletions test/github-issues/2904/issue-2904.ts
@@ -0,0 +1,14 @@
import {DeepPartial} from "../../../src";
import {Comment} from "./entity/Comment";

describe("github issues > #2904 Type DeepPartial issue when used with generics", () => {

it("DeepPartial should correctly handle generics", () => {
function commentFactory<E extends Comment>(entity: DeepPartial<E>) {
entity.createdAt = new Date();
entity.savedBy = 'SomeUSer';
}

commentFactory({});
});
});
12 changes: 6 additions & 6 deletions test/github-issues/7079/issue-7079.ts
Expand Up @@ -30,22 +30,22 @@ describe("github issues > #7079 Error when sorting by an embedded entity while u
id: 1,
text: "Happy Holidays",
userId: 1,
blog: { date: new Date().toISOString() },
newsletter: { date: new Date().toISOString() }
blog: { date: new Date() },
newsletter: { date: new Date() }
},
{
id: 2,
text: "My Vacation",
userId: 1,
blog: { date: new Date().toISOString() },
newsletter: { date: new Date().toISOString() }
blog: { date: new Date() },
newsletter: { date: new Date() }
},
{
id: 3,
text: "Working with TypeORM",
userId: 2,
blog: { date: new Date().toISOString() },
newsletter: { date: new Date().toISOString() }
blog: { date: new Date() },
newsletter: { date: new Date() }
}
]
await postRepo.save(posts);
Expand Down

0 comments on commit b93416d

Please sign in to comment.