Skip to content

Commit

Permalink
rollback #1917 as there is an issue with circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Dec 23, 2022
1 parent d88f722 commit 3c1ab2e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 110 deletions.
42 changes: 8 additions & 34 deletions type-definitions/immutable.d.ts
Expand Up @@ -91,30 +91,6 @@
*/

declare namespace Immutable {
/**
* @ignore
*/
export type DeepCopy<T> = T extends Collection.Keyed<infer KeyedKey, infer V>
? // convert KeyedCollection to DeepCopy plain JS object
{
[key in KeyedKey extends string | number | symbol
? KeyedKey
: string]: DeepCopy<V>;
}
: // convert IndexedCollection or Immutable.Set to DeepCopy plain JS array
T extends Collection<infer _, infer V>
? Array<DeepCopy<V>>
: T extends string | number // Iterable scalar types : should be kept as is
? T
: T extends Iterable<infer V> // Iterable are converted to plain JS array
? Array<DeepCopy<V>>
: T extends object // plain JS object are converted deeply
? {
[ObjectKey in keyof T]: DeepCopy<T[ObjectKey]>;
}
: // other case : should be kept as is
T;

/**
* Lists are ordered indexed dense collections, much like a JavaScript
* Array.
Expand Down Expand Up @@ -2690,7 +2666,7 @@ declare namespace Immutable {
* Note: This method may not be overridden. Objects with custom
* serialization to plain JS may override toJSON() instead.
*/
toJS(): DeepCopy<TProps>;
toJS(): { [K in keyof TProps]: unknown };

/**
* Shallowly converts this Record to equivalent native JavaScript Object.
Expand Down Expand Up @@ -2850,7 +2826,7 @@ declare namespace Immutable {
*
* Converts keys to Strings.
*/
toJS(): { [key in string | number | symbol]: DeepCopy<V> };
toJS(): { [key in string | number | symbol]: unknown };

/**
* Shallowly converts this Keyed Seq to equivalent native JavaScript Object.
Expand Down Expand Up @@ -2992,7 +2968,7 @@ declare namespace Immutable {
/**
* Deeply converts this Indexed Seq to equivalent native JavaScript Array.
*/
toJS(): Array<DeepCopy<T>>;
toJS(): Array<unknown>;

/**
* Shallowly converts this Indexed Seq to equivalent native JavaScript Array.
Expand Down Expand Up @@ -3167,7 +3143,7 @@ declare namespace Immutable {
/**
* Deeply converts this Set Seq to equivalent native JavaScript Array.
*/
toJS(): Array<DeepCopy<T>>;
toJS(): Array<unknown>;

/**
* Shallowly converts this Set Seq to equivalent native JavaScript Array.
Expand Down Expand Up @@ -3477,7 +3453,7 @@ declare namespace Immutable {
*
* Converts keys to Strings.
*/
toJS(): { [key in string | number | symbol]: DeepCopy<V> };
toJS(): { [key in string | number | symbol]: unknown };

/**
* Shallowly converts this Keyed collection to equivalent native JavaScript Object.
Expand Down Expand Up @@ -3659,7 +3635,7 @@ declare namespace Immutable {
/**
* Deeply converts this Indexed collection to equivalent native JavaScript Array.
*/
toJS(): Array<DeepCopy<T>>;
toJS(): Array<unknown>;

/**
* Shallowly converts this Indexed collection to equivalent native JavaScript Array.
Expand Down Expand Up @@ -3970,7 +3946,7 @@ declare namespace Immutable {
/**
* Deeply converts this Set collection to equivalent native JavaScript Array.
*/
toJS(): Array<DeepCopy<T>>;
toJS(): Array<unknown>;

/**
* Shallowly converts this Set collection to equivalent native JavaScript Array.
Expand Down Expand Up @@ -4232,9 +4208,7 @@ declare namespace Immutable {
* `Collection.Indexed`, and `Collection.Set` become `Array`, while
* `Collection.Keyed` become `Object`, converting keys to Strings.
*/
toJS():
| Array<DeepCopy<V>>
| { [key in string | number | symbol]: DeepCopy<V> };
toJS(): Array<unknown> | { [key in string | number | symbol]: unknown };

/**
* Shallowly converts this Collection to equivalent native JavaScript Array or Object.
Expand Down
70 changes: 0 additions & 70 deletions type-definitions/ts-tests/deepCopy.ts

This file was deleted.

3 changes: 2 additions & 1 deletion type-definitions/ts-tests/list.ts
Expand Up @@ -448,7 +448,8 @@ import {
{
// #toJS / #toJSON

// $ExpectType number[][]
// should be `number[][]` but there is an issue with deep conversion and circular references
// $ExpectType unknown[]
List<List<number>>().toJS();

// $ExpectType List<number>[]
Expand Down
3 changes: 2 additions & 1 deletion type-definitions/ts-tests/map.ts
Expand Up @@ -492,6 +492,7 @@ import { Map, List } from 'immutable';
{
// #toJS

// $ExpectType { [x: string]: number; [x: number]: number; [x: symbol]: number; }
// should be `{ [x: string]: number; [x: number]: number; [x: symbol]: number; }` but there is an issue with circular references
// $ExpectType { [x: string]: unknown; [x: number]: unknown; [x: symbol]: unknown; }
Map<number, number>().toJS();
}
9 changes: 6 additions & 3 deletions type-definitions/ts-tests/record.ts
Expand Up @@ -27,7 +27,8 @@ import { List, Map, Record, Set } from 'immutable';
// $ExpectError
pointXY.y = 10;

// $ExpectType { x: number; y: number; }
// should be `{ x: number; y: number; }` but there is an issue with circular references
// $ExpectType { x: unknown; y: unknown; }
pointXY.toJS();

class PointClass extends PointXY {
Expand Down Expand Up @@ -60,7 +61,8 @@ import { List, Map, Record, Set } from 'immutable';
// $ExpectType { x: number; y: number; }
point.toJSON();

// $ExpectType { x: number; y: number; }
// should be `{ x: number; y: number; }` but there is an issue with circular references
// $ExpectType { x: unknown; y: unknown; }
point.toJS();
}

Expand Down Expand Up @@ -88,6 +90,7 @@ import { List, Map, Record, Set } from 'immutable';
// $ExpectType { map: Map<string, string>; list: List<string>; set: Set<string>; }
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();
}
3 changes: 2 additions & 1 deletion type-definitions/ts-tests/set.ts
Expand Up @@ -286,7 +286,8 @@ import { Set, Map } from 'immutable';
{
// #toJS / #toJJSON

// $ExpectType number[][]
// should be `number[][]` but there is an issue with circular references
// $ExpectType unknown[]
Set<Set<number>>().toJS();

// $ExpectType Set<number>[]
Expand Down

0 comments on commit 3c1ab2e

Please sign in to comment.