Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve DeepPartial type (#8187)
* 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
1 parent
a11c50d
commit b93416d
Showing
5 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
} from "../../../../src"; | ||
|
||
export class Comment { | ||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@Column() | ||
savedBy: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters