Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type infererence for fromJS #1927

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion type-definitions/immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5160,12 +5160,39 @@ declare namespace Immutable {
*/
function fromJS(
jsValue: unknown,
reviver?: (
reviver: (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No way this is correct - I never had problems skipping this argument in runtime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ntucker . That's an overload for the definition right after. It does say :

  • if the reviver method is set, then the return type is Collection<unknown, unknown>
  • if the reviver is not set of undefined, then we can calculate the return type

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may very well be a bug with typescript, but I'm fairly certain you'd still be interested in supporting typescript versions less than 5.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's continue in #1935 to avoid multiplying conversation in different threads

key: string | number,
sequence: Collection.Keyed<string, unknown> | Collection.Indexed<unknown>,
path?: Array<string | number>
) => unknown
): Collection<unknown, unknown>;
function fromJS<JSValue>(
jsValue: JSValue,
reviver?: undefined
): FromJS<JSValue>;

type FromJS<JSValue> = JSValue extends FromJSNoTransform
? JSValue
: JSValue extends Array<any>
? FromJSArray<JSValue>
: JSValue extends {}
? FromJSObject<JSValue>
: any;

type FromJSNoTransform =
| Collection<any, any>
| number
| string
| null
| undefined;

type FromJSArray<JSValue> = JSValue extends Array<infer T>
? List<FromJS<T>>
: never;

type FromJSObject<JSValue> = JSValue extends {}
? Map<keyof JSValue, FromJS<JSValue[keyof JSValue]>>
: never;

/**
* Value equality check with semantics similar to `Object.is`, but treats
Expand Down
35 changes: 35 additions & 0 deletions type-definitions/ts-tests/from-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { fromJS, List, Map } from 'immutable';

{
// fromJS

// $ExpectType Collection<unknown, unknown>
fromJS({}, (a: any, b: any) => b);

// $ExpectType string
fromJS('abc');

// $ExpectType List<number>
fromJS([0, 1, 2]);

// $ExpectType List<number>
fromJS(List([0, 1, 2]));

// $ExpectType Map<"b" | "a" | "c", number>
fromJS({a: 0, b: 1, c: 2});

// $ExpectType Map<string, number>
fromJS(Map({a: 0, b: 1, c: 2}));

// $ExpectType List<Map<"a", number>>
fromJS([{a: 0}]);

// $ExpectType Map<"a", List<number>>
fromJS({a: [0]});

// $ExpectType List<List<List<number>>>
fromJS([[[0]]]);

// $ExpectType Map<"a", Map<"b", Map<"c", number>>>
fromJS({a: {b: {c: 0}}});
}