diff --git a/CHANGELOG.md b/CHANGELOG.md index c7d2d7cb5..799bd8144 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -156,6 +156,7 @@ All the random notes that should make up a nice changelog: * Dropped already deprecated toplevel `map` function * Killed the already deprecated modifiers `asFlat` etc. If you war still using this, see the MobX 2 -> 3 migration notes. * `autorun` now accepts a scheduler function to allow improved performance for tasks such as rendering to canvas +* Observable maps now fully implement the map interface. See [#1361](https://github.com/mobxjs/mobx/pull/1361) by [Marc Fallows](https://github.com/marcfallows) # 3.6.1 diff --git a/src/types/observablemap.ts b/src/types/observablemap.ts index 6d6407e6d..24e0a8c20 100644 --- a/src/types/observablemap.ts +++ b/src/types/observablemap.ts @@ -10,7 +10,8 @@ import { invariant, isES6Map, getMapLikeKeys, - fail + fail, + addHiddenFinalProp } from "../utils/utils" import { IInterceptable, @@ -78,7 +79,8 @@ export class ObservableMap interceptors changeListeners dehancer: any; - [Symbol.iterator] + [Symbol.iterator]; + [Symbol.toStringTag] constructor( initialData?: IObservableMapInitialValues, @@ -374,6 +376,12 @@ declareIterator(ObservableMap.prototype, function() { return this.entries() }) +addHiddenFinalProp( + ObservableMap.prototype, + typeof Symbol !== "undefined" ? Symbol.toStringTag : "@@toStringTag" as any, + "Map" +) + /* 'var' fixes small-build issue */ export var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap) as ( thing: any diff --git a/test/base/map.js b/test/base/map.js index bb6c342a8..ba77e0ab8 100644 --- a/test/base/map.js +++ b/test/base/map.js @@ -678,3 +678,9 @@ test("maps.values, keys and maps.entries are iterables", () => { expect(Array.from(x.values())).toEqual([1, 2]) expect(Array.from(x.keys())).toEqual(["x", "y"]) }) + +test("toStringTag", () => { + const x = mobx.observable.map({ x: 1, y: 2 }) + expect(x[Symbol.toStringTag]).toBe("Map") + expect(Object.prototype.toString.call(x)).toBe("[object Map]") +}) diff --git a/test/base/typescript-tests.ts b/test/base/typescript-tests.ts index a2a3f2ad4..c33139c24 100644 --- a/test/base/typescript-tests.ts +++ b/test/base/typescript-tests.ts @@ -1499,3 +1499,9 @@ test.skip("actions are reassignable", () => { a.m2 = () => {} expect(isAction(a.m2)).toBe(true) }) + +test("map should structurally match ES6 Map", () => { + // Including this line strictly for type checking. + const m: Map = mobx.observable.map({ a: 1, b: 2 }) + expect(true).toBe(true) +})