Skip to content

Commit

Permalink
Allow using 'as const' for creating tuples for map.replace (#3344)
Browse files Browse the repository at this point in the history
* Allow using 'as const' for creating tuples for map.replace

* add changeset

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Update tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 authored Jul 19, 2022
1 parent 1581f14 commit b375535
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-boats-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Allow readonly tuples as part of IObservableMapInitialValues
15 changes: 15 additions & 0 deletions packages/mobx/__tests__/v5/base/typescript-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,21 @@ test("flow support throwing async generators", async () => {
}
})

test("allow 'as const' for creating tuples for map.replace()", () => {
const map = mobx.observable.map<string, number>()
const srcValues = mobx.observable.array<string>()
const newValues = Array.from(srcValues, (value, index) => [value, index] as const)

map.replace(newValues)
})

test("allow 'as const' for creating tuples for observable.map()", () => {
const srcValues = mobx.observable.array<string>()
const newValues = Array.from(srcValues, (value, index) => [value, index] as const)

mobx.observable.map(newValues)
})

test("toJS bug #1413 (TS)", () => {
class X {
test = {
Expand Down
6 changes: 4 additions & 2 deletions packages/mobx/src/types/observablemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export interface IKeyValueMap<V = any> {
}

export type IMapEntry<K = any, V = any> = [K, V]
export type IReadonlyMapEntry<K = any, V = any> = readonly [K, V]
export type IMapEntries<K = any, V = any> = IMapEntry<K, V>[]
export type IReadonlyMapEntries<K = any, V = any> = IReadonlyMapEntry<K, V>[]

export type IMapDidChange<K = any, V = any> = { observableKind: "map"; debugObjectName: string } & (
| {
Expand Down Expand Up @@ -81,14 +83,14 @@ export const DELETE = "delete"

export type IObservableMapInitialValues<K = any, V = any> =
| IMapEntries<K, V>
| IReadonlyMapEntries<K, V>
| IKeyValueMap<V>
| Map<K, V>

// just extend Map? See also https://gist.github.com/nestharus/13b4d74f2ef4a2f4357dbd3fc23c1e54
// But: https://github.com/mobxjs/mobx/issues/1556
export class ObservableMap<K = any, V = any>
implements Map<K, V>, IInterceptable<IMapWillChange<K, V>>, IListenable
{
implements Map<K, V>, IInterceptable<IMapWillChange<K, V>>, IListenable {
[$mobx] = ObservableMapMarker
data_: Map<K, ObservableValue<V>>
hasMap_: Map<K, ObservableValue<boolean>> // hasMap, not hashMap >-).
Expand Down

0 comments on commit b375535

Please sign in to comment.