Skip to content
This repository was archived by the owner on Jul 23, 2021. It is now read-only.

Commit faee487

Browse files
committed
Rename MapFromObject to MapOf to match RecordOf
1 parent ad0356b commit faee487

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This made `Map` really unusable with TypeScript.
2727
Now the Map is typed like this:
2828

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

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

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

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

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

7879
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.
7980

80-
81-
8281
## [4.2.4] - 2023-02-06
8382

8483
- Improve type infererence for from JS by [KSXGitHub](https://github.com/KSXGitHub) [#1927](https://github.com/immutable-js/immutable-js/pull/1927)

type-definitions/immutable.d.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ declare namespace Immutable {
812812
function Map<K, V>(collection?: Iterable<[K, V]>): Map<K, V>;
813813
function Map<R extends { [key in string | number | symbol]: unknown }>(
814814
obj: R
815-
): MapFromObject<R>;
815+
): MapOf<R>;
816816
function Map<V>(obj: { [key: string]: V }): Map<string, V>;
817817
function Map<K extends string | symbol, V>(obj: { [P in K]?: V }): Map<K, V>;
818818

@@ -821,9 +821,8 @@ declare namespace Immutable {
821821
*
822822
* @ignore
823823
*/
824-
interface MapFromObject<
825-
R extends { [key in string | number | symbol]: unknown }
826-
> extends Map<keyof R, R[keyof R]> {
824+
interface MapOf<R extends { [key in string | number | symbol]: unknown }>
825+
extends Map<keyof R, R[keyof R]> {
827826
/**
828827
* Returns the value associated with the provided key, or notSetValue if
829828
* the Collection does not contain this key.
@@ -851,8 +850,8 @@ declare namespace Immutable {
851850
updater: (value: R[K]) => R[K]
852851
): this;
853852

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

867866
/** @ignore */
868-
type GetMapType<S> = S extends MapFromObject<infer T> ? T : S;
867+
type GetMapType<S> = S extends MapOf<infer T> ? T : S;
869868

870869
/** @ignore */
871870
type Head<T extends ReadonlyArray<any>> = T extends [

type-definitions/ts-tests/covariance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let listOfC: List<C> = listOfB;
3434
declare var mapOfB: Map<string, B>;
3535
let mapOfA: Map<string, A> = mapOfB;
3636

37-
// $ExpectType MapFromObject<{ b: B; }>
37+
// $ExpectType MapOf<{ b: B; }>
3838
mapOfA = Map({ b: new B() });
3939

4040
// $ExpectError

type-definitions/ts-tests/map.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Map, List, MapFromObject } from 'immutable';
1+
import { Map, List, MapOf } from 'immutable';
22

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

21-
// $ExpectType MapFromObject<{ a: number; }>
21+
// $ExpectType MapOf<{ a: number; }>
2222
Map({ a: 1 });
2323

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

27-
// $ExpectType MapFromObject<{ a: MapFromObject<{ b: MapFromObject<{ c: number; }>; }>; }>
27+
// $ExpectType MapOf<{ a: MapOf<{ b: MapOf<{ c: number; }>; }>; }>
2828
Map({ a: Map({ b: Map({ c: 3 }) }) });
2929

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

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

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

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

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

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

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

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

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

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

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

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

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

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

293293
// $ExpectError

0 commit comments

Comments
 (0)