Skip to content

Commit

Permalink
fix: DeepReadonly for a union with an array of itself (#306) (#310)
Browse files Browse the repository at this point in the history
* fix: DeepReadonly for a union with an array of itself (#306)

* chore: fix styling according to prettier

* chore: fix handling of tuples

* chore: fix styling according to prettier

* chore: add changeset
  • Loading branch information
psmolinsky committed Jul 14, 2022
1 parent 36f78f6 commit 8a28c53
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-oranges-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ts-essentials": patch
---

Fix `DeepReadonly` for a union with an array of itself
4 changes: 4 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ export type DeepReadonly<T> = T extends Builtin
? WeakSet<DeepReadonly<U>>
: T extends Promise<infer U>
? Promise<DeepReadonly<U>>
: T extends AnyArray<infer U>
? T extends IsTuple<T>
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: ReadonlyArray<DeepReadonly<U>>
: T extends {}
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: IsUnknown<T> extends true
Expand Down
11 changes: 11 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ function testDeepReadonly() {
Assert<IsExact<DeepReadonly<ComplexNestedRequired>, ComplexNestedReadonly>>,
];

// Build-time test to ensure the fix for
// https://github.com/ts-essentials/ts-essentials/pull/310
// because IsExact<> is unable to text it.
{
type TestUnion = { value: string } | TestUnion[];
type ReadonlyTestUnion = { readonly value: string } | readonly ReadonlyTestUnion[];

const a: DeepReadonly<TestUnion> = [];
const b: ReadonlyTestUnion = a;
}

// Build-time test to ensure the fix for
// https://github.com/krzkaczor/ts-essentials/issues/17 remains in place.
{
Expand Down

0 comments on commit 8a28c53

Please sign in to comment.