From 1e2ec73c9fd78e480cfba598786cbc70034f5c3d Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Fri, 25 Aug 2023 15:37:28 +0200 Subject: [PATCH 1/5] Rollback toJS type due to circular reference error (#1958) --- type-definitions/immutable.d.ts | 18 ++++++-- type-definitions/ts-tests/deepCopy.ts | 59 ++++++++++++++++++++++++--- type-definitions/ts-tests/record.ts | 6 ++- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/type-definitions/immutable.d.ts b/type-definitions/immutable.d.ts index 0331a54bc..7bd59b2e2 100644 --- a/type-definitions/immutable.d.ts +++ b/type-definitions/immutable.d.ts @@ -91,6 +91,16 @@ */ declare namespace Immutable { + /** @ignore */ + type OnlyObject = Extract; + + /** @ignore */ + type ContainObject = OnlyObject extends object + ? OnlyObject extends never + ? false + : true + : false; + /** * @ignore * @@ -100,14 +110,14 @@ declare namespace Immutable { export type DeepCopy = T extends Record ? // convert Record to DeepCopy plain JS object { - [key in keyof R]: DeepCopy; + [key in keyof R]: ContainObject extends true ? unknown : R[key]; } : T extends Collection.Keyed ? // convert KeyedCollection to DeepCopy plain JS object { [key in KeyedKey extends string | number | symbol ? KeyedKey - : string]: DeepCopy; + : string]: V extends object ? unknown : V; } : // convert IndexedCollection or Immutable.Set to DeepCopy plain JS array T extends Collection @@ -118,7 +128,9 @@ declare namespace Immutable { ? Array> : T extends object // plain JS object are converted deeply ? { - [ObjectKey in keyof T]: DeepCopy; + [ObjectKey in keyof T]: ContainObject extends true + ? unknown + : T[ObjectKey]; } : // other case : should be kept as is T; diff --git a/type-definitions/ts-tests/deepCopy.ts b/type-definitions/ts-tests/deepCopy.ts index dc64f1533..a3240d3c1 100644 --- a/type-definitions/ts-tests/deepCopy.ts +++ b/type-definitions/ts-tests/deepCopy.ts @@ -23,10 +23,12 @@ import { List, Map, Record, Set, Seq, DeepCopy, Collection } from 'immutable'; // $ExpectType { [x: string]: string; } type StringKey = DeepCopy>; - // $ExpectType { [x: string]: object; } + // should be `{ [x: string]: object; }` but there is an issue with circular references + // $ExpectType { [x: string]: unknown; } type ObjectKey = DeepCopy>; - // $ExpectType { [x: string]: object; [x: number]: object; } + // should be `{ [x: string]: object; [x: number]: object; }` but there is an issue with circular references + // $ExpectType { [x: string]: unknown; [x: number]: unknown; } type MixedKey = DeepCopy>; // $ExpectType string[] @@ -55,10 +57,12 @@ import { List, Map, Record, Set, Seq, DeepCopy, Collection } from 'immutable'; { // Nested - // $ExpectType { map: { [x: string]: string; }; list: string[]; set: string[]; } + // should be `{ map: { [x: string]: string; }; list: string[]; set: string[]; }` but there is an issue with circular references + // $ExpectType { map: unknown; list: unknown; set: unknown; } type NestedObject = DeepCopy<{ map: Map; list: List; set: Set; }>; - // $ExpectType { map: { [x: string]: string; }; } + // should be `{ map: { [x: string]: string; }; }`, but there is an issue with circular references + // $ExpectType { map: unknown; } type NestedMap = DeepCopy>>; } @@ -68,6 +72,51 @@ import { List, Map, Record, Set, Seq, DeepCopy, Collection } from 'immutable'; type Article = Record<{ title: string; tag: Tag; }>; type Tag = Record<{ name: string; article: Article; }>; - // $ExpectType { title: string; tag: { name: string; article: any; }; } + // should handle circular references here somehow + // $ExpectType { title: string; tag: unknown; } type Circular = DeepCopy
; } + +{ + // Circular references #1957 + + class Foo1 extends Record<{ + foo: undefined | Foo1; + }>({ + foo: undefined + }) { + } + + class Foo2 extends Record<{ + foo?: Foo2; + }>({ + foo: undefined + }) { + } + + class Foo3 extends Record<{ + foo: null | Foo3; + }>({ + foo: null + }) { + } + + // $ExpectType { foo: unknown; } + type DeepFoo1 = DeepCopy; + + // $ExpectType { foo?: unknown; } + type DeepFoo2 = DeepCopy; + + // $ExpectType { foo: unknown; } + type DeepFoo3 = DeepCopy; + + class FooWithList extends Record<{ + foos: undefined | List; + }>({ + foos: undefined + }) { + } + + // $ExpectType { foos: unknown; } + type DeepFooList = DeepCopy; +} diff --git a/type-definitions/ts-tests/record.ts b/type-definitions/ts-tests/record.ts index c245cf310..559ad30f9 100644 --- a/type-definitions/ts-tests/record.ts +++ b/type-definitions/ts-tests/record.ts @@ -88,7 +88,8 @@ import { List, Map, Record, RecordOf, Set } from 'immutable'; // $ExpectType { map: Map; list: List; set: Set; } withMap.toJSON(); - // $ExpectType { map: { [x: string]: string; }; list: string[]; set: string[]; } + // should be `{ map: { [x: string]: string; }; list: string[]; set: string[]; }` but there is an issue with circular references + // $ExpectType { map: unknown; list: unknown; set: unknown; } withMap.toJS(); } @@ -101,7 +102,8 @@ import { List, Map, Record, RecordOf, Set } from 'immutable'; const line = Line({}); - // $ExpectType { size?: { distance: string; } | undefined; color?: string | undefined; } + // should be { size?: { distance: string; } | undefined; color?: string | undefined; } but there is an issue with circular references + // $ExpectType { size?: unknown; color?: string | undefined; } line.toJS(); } From 4d4f004a1a2c74c07cdcc93f4ea31f22cfe9f2cb Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Fri, 25 Aug 2023 13:38:34 +0000 Subject: [PATCH 2/5] update changelog for 4.3.4 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1f82c4a7..b4555dafc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ Dates are formatted as YYYY-MM-DD. ## Unreleased +## [4.3.4] - 2023-08-25 + +- Rollback toJS type due to circular reference error [#1958](https://github.com/immutable-js/immutable-js/pull/1958) by [@jdeniau](https://github.com/jdeniau) + ## [4.3.3] - 2023-08-23 - [typescript] manage to handle toJS circular reference. [#1932](https://github.com/immutable-js/immutable-js/pull/1932) by [@jdeniau](https://github.com/jdeniau) From fdfc97514f1b288301c7a1ea73873bbd4c832ffb Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Fri, 25 Aug 2023 13:37:55 +0000 Subject: [PATCH 3/5] 4.3.4 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 85107a003..9d14d7d7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "immutable", - "version": "4.3.3", + "version": "4.3.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "immutable", - "version": "4.3.3", + "version": "4.3.4", "license": "MIT", "devDependencies": { "@types/jest": "27.0.1", diff --git a/package.json b/package.json index 34b552c4e..b66eaf482 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "immutable", - "version": "4.3.3", + "version": "4.3.4", "description": "Immutable Data Collections", "license": "MIT", "homepage": "https://immutable-js.com", From 8cb4e535cca858fc8b54fb7536448ed9556ced73 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 28 Aug 2023 12:24:43 +0000 Subject: [PATCH 4/5] 5.0.0-beta.3 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9d14d7d7e..b76c2f5de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "immutable", - "version": "4.3.4", + "version": "5.0.0-beta.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "immutable", - "version": "4.3.4", + "version": "5.0.0-beta.3", "license": "MIT", "devDependencies": { "@types/jest": "27.0.1", diff --git a/package.json b/package.json index b66eaf482..4c258af22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "immutable", - "version": "4.3.4", + "version": "5.0.0-beta.3", "description": "Immutable Data Collections", "license": "MIT", "homepage": "https://immutable-js.com", From 2e95835fe8e9517745f025c29131355d6956acd7 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 28 Aug 2023 12:47:09 +0000 Subject: [PATCH 5/5] Revert "5.0.0-beta.3" This reverts commit 8cb4e535cca858fc8b54fb7536448ed9556ced73. --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b76c2f5de..9d14d7d7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "immutable", - "version": "5.0.0-beta.3", + "version": "4.3.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "immutable", - "version": "5.0.0-beta.3", + "version": "4.3.4", "license": "MIT", "devDependencies": { "@types/jest": "27.0.1", diff --git a/package.json b/package.json index 4c258af22..b66eaf482 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "immutable", - "version": "5.0.0-beta.3", + "version": "4.3.4", "description": "Immutable Data Collections", "license": "MIT", "homepage": "https://immutable-js.com",