Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using 'as const' for creating tuples for map.replace #3344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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