Skip to content

Commit

Permalink
Rename MapFromObject to MapOf to match RecordOf
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Feb 10, 2023
1 parent ad0356b commit beeac64
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Expand Up @@ -27,7 +27,7 @@ This made `Map` really unusable with TypeScript.
Now the Map is typed like this:

```ts
MapFromObject<{
MapOf<{
length: number;
1: string;
}>
Expand All @@ -38,6 +38,7 @@ and the return type of `m.get('length')` is typed as `number`.
The return of `m.get('inexistant')` throw the TypeScript error:

> Argument of type '"inexistant"' is not assignable to parameter of type '1 | "length"
#### If you want to keep the old definition

**This is a minor BC for TS users**, so if you want to keep the old definition, you can declare you Map like this:
Expand All @@ -59,7 +60,7 @@ type MyMapType = {
const m = Map<MyMapType>({ length: 3, 1: 'one' });
```

Keep in mind that the `MapFromObject` will try to be consistant with the simple TypeScript object, so you can not do this:
Keep in mind that the `MapOf` will try to be consistant with the simple TypeScript object, so you can not do this:

```ts
Map({ a: 'a' }).set('b', 'b');
Expand All @@ -77,8 +78,6 @@ Map<{ a?: string }>({ a: 'a' }).delete('a'); // you can only delete an optional

For now, only `get`, `getIn`, `set`, `update`, `delete` and `remove` methods are implemented. All other methods will fallback to the basic `Map` definition. Other method definition will be added later, but as some might be really complex, we prefer the progressive enhancement on the most used functions.



## [4.2.4] - 2023-02-06

- Improve type infererence for from JS by [KSXGitHub](https://github.com/KSXGitHub) [#1927](https://github.com/immutable-js/immutable-js/pull/1927)
Expand Down
13 changes: 6 additions & 7 deletions type-definitions/immutable.d.ts
Expand Up @@ -812,7 +812,7 @@ declare namespace Immutable {
function Map<K, V>(collection?: Iterable<[K, V]>): Map<K, V>;
function Map<R extends { [key in string | number | symbol]: unknown }>(
obj: R
): MapFromObject<R>;
): MapOf<R>;
function Map<V>(obj: { [key: string]: V }): Map<string, V>;
function Map<K extends string | symbol, V>(obj: { [P in K]?: V }): Map<K, V>;

Expand All @@ -821,9 +821,8 @@ declare namespace Immutable {
*
* @ignore
*/
interface MapFromObject<
R extends { [key in string | number | symbol]: unknown }
> extends Map<keyof R, R[keyof R]> {
interface MapOf<R extends { [key in string | number | symbol]: unknown }>
extends Map<keyof R, R[keyof R]> {
/**
* Returns the value associated with the provided key, or notSetValue if
* the Collection does not contain this key.
Expand Down Expand Up @@ -851,8 +850,8 @@ declare namespace Immutable {
updater: (value: R[K]) => R[K]
): this;

// Possible best type is MapFromObject<Omit<R, K>> but Omit seems to broke other function calls
// and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapFromObject
// Possible best type is MapOf<Omit<R, K>> but Omit seems to broke other function calls
// and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapOf
delete<K extends keyof R>(
key: K
): Extract<R[K], undefined> extends never ? never : this;
Expand All @@ -865,7 +864,7 @@ declare namespace Immutable {
// https://github.com/immutable-js/immutable-js/issues/1462#issuecomment-584123268

/** @ignore */
type GetMapType<S> = S extends MapFromObject<infer T> ? T : S;
type GetMapType<S> = S extends MapOf<infer T> ? T : S;

/** @ignore */
type Head<T extends ReadonlyArray<any>> = T extends [
Expand Down
2 changes: 1 addition & 1 deletion type-definitions/ts-tests/covariance.ts
Expand Up @@ -34,7 +34,7 @@ let listOfC: List<C> = listOfB;
declare var mapOfB: Map<string, B>;
let mapOfA: Map<string, A> = mapOfB;

// $ExpectType MapFromObject<{ b: B; }>
// $ExpectType MapOf<{ b: B; }>
mapOfA = Map({ b: new B() });

// $ExpectError
Expand Down
2 changes: 1 addition & 1 deletion type-definitions/ts-tests/from-js.ts
Expand Up @@ -18,7 +18,7 @@ import { fromJS, List, Map } from 'immutable';
// $ExpectType Map<"b" | "a" | "c", number>
fromJS({a: 0, b: 1, c: 2});

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

// $ExpectType List<Map<"a", number>>
Expand Down
4 changes: 2 additions & 2 deletions type-definitions/ts-tests/groupBy.ts
Expand Up @@ -28,8 +28,8 @@ import { List, Map, OrderedMap, Record, Set, Seq, Stack, OrderedSet, DeepCopy, C
// $ExpectType Map<string, Map<string, number>>
Map<string, number>({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`);

// type should be something like Map<string, MapFromObject<Partial{ a: number, b: number, c: number, d: number }>>> but groupBy returns a wrong type with `this`
// $ExpectType Map<string, MapFromObject<{ a: number; b: number; c: number; d: number; }>>
// type should be something like Map<string, MapOf<Partial{ a: number, b: number, c: number, d: number }>>> but groupBy returns a wrong type with `this`
// $ExpectType Map<string, MapOf<{ a: number; b: number; c: number; d: number; }>>
Map({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`);

// $ExpectType Map<string, OrderedMap<string, number>>
Expand Down
26 changes: 13 additions & 13 deletions type-definitions/ts-tests/map.ts
@@ -1,4 +1,4 @@
import { Map, List, MapFromObject } from 'immutable';
import { Map, List, MapOf } from 'immutable';

{
// #constructor
Expand All @@ -18,13 +18,13 @@ import { Map, List, MapFromObject } from 'immutable';
// $ExpectType Map<number, string>
Map(List<[number, string]>([[1, 'a']]));

// $ExpectType MapFromObject<{ a: number; }>
// $ExpectType MapOf<{ a: number; }>
Map({ a: 1 });

// $ExpectType MapFromObject<{ a: number; b: string; }>
// $ExpectType MapOf<{ a: number; b: string; }>
Map({ a: 1, b: 'b' });

// $ExpectType MapFromObject<{ a: MapFromObject<{ b: MapFromObject<{ c: number; }>; }>; }>
// $ExpectType MapOf<{ a: MapOf<{ b: MapOf<{ c: number; }>; }>; }>
Map({ a: Map({ b: Map({ c: 3 }) }) });

// $ExpectError
Expand Down Expand Up @@ -119,7 +119,7 @@ import { Map, List, MapFromObject } from 'immutable';
// $ExpectType number
Map({ a: Map({ b: Map({ c: Map({ d: 4 }) }) }) }).getIn(['a', 'b', 'c', 'd']);

// with a better type, it should be resolved to `number` in the future. `RetrievePathReducer` does not work with anything else than MapFromObject
// with a better type, it should be resolved to `number` in the future. `RetrievePathReducer` does not work with anything else than MapOf
// $ExpectType never
Map({ a: List([ 1 ]) }).getIn(['a', 0]);
}
Expand All @@ -145,10 +145,10 @@ import { Map, List, MapFromObject } from 'immutable';
// $ExpectError
Map({ a: 1 }).set('b', 'b');

// $ExpectType MapFromObject<{ a: number; b?: string | undefined; }>
// $ExpectType MapOf<{ a: number; b?: string | undefined; }>
Map<{ a: number; b?: string; }>({ a: 1 }).set('b', 'b');

// $ExpectType MapFromObject<{ a: number; b?: string | undefined; }>
// $ExpectType MapOf<{ a: number; b?: string | undefined; }>
Map<{ a: number; b?: string; }>({ a: 1 }).set('b', undefined);

// $ExpectType number
Expand All @@ -161,7 +161,7 @@ import { Map, List, MapFromObject } from 'immutable';
phone: 'bar',
});

// $ExpectType MapFromObject<{ phone: string | number; }>
// $ExpectType MapOf<{ phone: string | number; }>
customer = customer.set('phone', 8);
}

Expand All @@ -184,10 +184,10 @@ import { Map, List, MapFromObject } from 'immutable';
// $ExpectType never
Map({ a: 1, b: 'b' }).delete('b');

// $ExpectType MapFromObject<{ a: number; b?: string | undefined; }>
// $ExpectType MapOf<{ a: number; b?: string | undefined; }>
Map<{ a: number; b?: string; }>({ a: 1, b: 'b' }).delete('b');

// $ExpectType MapFromObject<{ a?: number | undefined; b?: string | undefined; }>
// $ExpectType MapOf<{ a?: number | undefined; b?: string | undefined; }>
Map<{ a?: number; b?: string; }>({ a: 1, b: 'b' }).remove('b').delete('a');

// $ExpectType number
Expand Down Expand Up @@ -278,16 +278,16 @@ import { Map, List, MapFromObject } from 'immutable';
// $ExpectError
Map({ a: 1, b: 'b' }).update('c', (v) => v);

// $ExpectType MapFromObject<{ a: number; b: string; }>
// $ExpectType MapOf<{ a: number; b: string; }>
Map({ a: 1, b: 'b' }).update('b', (v) => v.toUpperCase());

// $ExpectType MapFromObject<{ a: number; b: string; }>
// $ExpectType MapOf<{ a: number; b: string; }>
Map({ a: 1, b: 'b' }).update('b', 'NSV', (v) => v.toUpperCase());

// $ExpectError
Map({ a: 1, b: 'b' }).update((v) => ({ a: 'a' }));

// $ExpectType MapFromObject<{ a: number; b: string; }>
// $ExpectType MapOf<{ a: number; b: string; }>
Map({ a: 1, b: 'b' }).update((v) => v.set('a', 2).set('b', 'B'));

// $ExpectError
Expand Down
2 changes: 1 addition & 1 deletion type-definitions/ts-tests/record.ts
Expand Up @@ -85,7 +85,7 @@ import { List, Map, Record, Set } from 'immutable';

const withMap = WithMap();

// $ExpectType { map: MapFromObject<{ a: string; }>; list: List<string>; set: Set<string>; }
// $ExpectType { map: MapOf<{ a: string; }>; list: List<string>; set: Set<string>; }
withMap.toJSON();

// should be `{ map: { [x: string]: string; }; list: string[]; set: string[]; }` but there is an issue with circular references
Expand Down

0 comments on commit beeac64

Please sign in to comment.