10000 Add common pitfall doc to fromJS · sjfloat/immutable-js@ecae145 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecae145

Browse files
committed
Add common pitfall doc to fromJS
1 parent 705e3e7 commit ecae145

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

dist/immutable.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ declare module Immutable {
6161
* arrays which pass `Array.isArray` to Lists, and only raw objects (no custom
6262
* prototype) to Map.
6363
*
64+
* Keep in mind, when using JS objects to construct Immutable Maps, that
65+
* JS object properties are always converted to strings, while Immutable Maps
66+
* accept keys of any type.
67+
*
68+
* var obj = { 1: "one" };
69+
* obj[1]; // "one"
70+
* obj["1"]; // "one"
71+
* Object.keys(obj); // [ "1" ]
72+
*
73+
* var map = fromJS(obj);
74+
* map.get(1); // undefined
75+
* map.get("1"); // "one"
76+
*
77+
* Property access for JS objects converts the key to a string, but Map keys
78+
* can be of any type, so the argument to `get()` is not converted.
79+
*
6480
* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
6581
* "Using the reviver parameter"
6682
*/
@@ -404,6 +420,21 @@ declare module Immutable {
404420
* var newMap = Map({key: "value"});
405421
* var newMap = Map([["key", "value"]]);
406422
*
423+
* Keep in mind, when using JS objects to construct Immutable Maps, that
424+
* JS object properties are always converted to strings, while Immutable Maps
425+
* accept keys of any type.
426+
*
427+
* var obj = { 1: "one" };
428+
* obj[1]; // "one"
429+
* obj["1"]; // "one" 10000
430+
* Object.keys(obj); // [ "1" ]
431+
*
432+
* var map = Map(obj);
433+
* map.get(1); // undefined
434+
* map.get("1"); // "one"
435+
*
436+
* Property access for JS objects converts the key to a string, but Map keys
437+
* can be of any type, so the argument to `get()` is not converted.
407438
*/
408439
export function Map<K, V>(): Map<K, V>;
409440
export function Map<K, V>(iter: KeyedIterable<K, V>): Map<K, V>;

type-definitions/Immutable.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ declare module Immutable {
6161
* arrays which pass `Array.isArray` to Lists, and only raw objects (no custom
6262
* prototype) to Map.
6363
*
64+
* Keep in mind, when using JS objects to construct Immutable Maps, that
65+
* JS object properties are always converted to strings, while Immutable Maps
66+
* accept keys of any type.
67+
*
68+
* var obj = { 1: "one" };
69+
* obj[1]; // "one"
70+
* obj["1"]; // "one"
71+
* Object.keys(obj); // [ "1" ]
72+
*
73+
* var map = fromJS(obj);
74+
* map.get(1); // undefined
75+
* map.get("1"); // "one"
76+
*
77+
* Property access for JS objects converts the key to a string, but Map keys
78+
* can be of any type, so the argument to `get()` is not converted.
79+
*
6480
* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
6581
* "Using the reviver parameter"
6682
*/
@@ -405,11 +421,13 @@ declare module Immutable {
405421
* var newMap = Map([["key", "value"]]);
406422
*
407423
* Keep in mind, when using JS objects to construct Immutable Maps, that
408-
* JS object properties are always converted to strings.
424+
* JS object properties are always converted to strings, while Immutable Maps
425+
* accept keys of any type.
409426
*
410427
* var obj = { 1: "one" };
411428
* obj[1]; // "one"
412429
* obj["1"]; // "one"
430+
* Object.keys(obj); // [ "1" ]
413431
*
414432
* var map = Map(obj);
415433
* map.get(1); // undefined

0 commit comments

Comments
 (0)
0