Skip to content

Commit 74f26b5

Browse files
authored
fix: Fix issue with wrong inferred type of observable.map in TS 5.9+ (#4578)
* Fix issue with wrong inferred type of observable.map in TS 5.9+ * initialValues can be optional
1 parent 2808e84 commit 74f26b5

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"resolutions": {
88
"jest": "^29.5.0",
9-
"typescript": "^5.6.2",
9+
"typescript": "^5.9.2",
1010
"recast": "^0.23.1"
1111
},
1212
"repository": {
@@ -68,7 +68,7 @@
6868
"tape": "^5.0.1",
6969
"ts-jest": "^29.0.5",
7070
"tsdx": "^0.14.1",
71-
"typescript": "^5.6.2"
71+
"typescript": "^5.9.2"
7272
},
7373
"husky": {
7474
"hooks": {

packages/mobx/__tests__/v5/base/typescript-tests.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,44 @@ test("allow 'as const' for creating tuples for observable.map()", () => {
19331933
mobx.observable.map(newValues)
19341934
})
19351935

1936+
test("infers tuple type for observable.map() when using array.map", () => {
1937+
interface Usage {
1938+
daily: number
1939+
monthly: number
1940+
}
1941+
const data: Array<{ id: string; usage: Usage }> = [
1942+
{ id: "1", usage: { daily: 5, monthly: 100 } },
1943+
{ id: "2", usage: { daily: 10, monthly: 200 } },
1944+
{ id: "3", usage: { daily: 15, monthly: 300 } }
1945+
]
1946+
1947+
const map = mobx.observable.map(
1948+
data.map(app => [app.id, app.usage]),
1949+
{ name: "test" }
1950+
)
1951+
1952+
expect(map.get("2")).toEqual({ daily: 10, monthly: 200 })
1953+
})
1954+
1955+
test("observable.map() accepts an undefined value with or without options", () => {
1956+
const map = mobx.observable.map<string, number>(undefined, { name: "custom name" })
1957+
map.set("test", 1)
1958+
expect(map.get("test")).toBe(1)
1959+
expect(map.name_).toBe("custom name")
1960+
1961+
// without options
1962+
mobx.observable.map(undefined)
1963+
})
1964+
1965+
test("observable.map() accepts optional initial values", () => {
1966+
interface Usage {}
1967+
1968+
;(data?: [string, Usage][]) => observable.map(data, { name: "test" })
1969+
;(data?: readonly (readonly [string, Usage])[]) => observable.map(data, { name: "test" })
1970+
;(data?: Record<string, Usage>) => observable.map(data, { name: "test" })
1971+
;(data?: Map<string, Usage>) => observable.map(data, { name: "test" })
1972+
})
1973+
19361974
test("toJS bug #1413 (TS)", () => {
19371975
class X {
19381976
test = {

packages/mobx/src/api/observable.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {
33
IEqualsComparer,
44
IObservableArray,
55
IObservableMapInitialValues,
6+
IMapEntries,
7+
IReadonlyMapEntries,
8+
IKeyValueMap,
69
IObservableSetInitialValues,
710
IObservableValue,
811
ObservableMap,
@@ -151,6 +154,24 @@ export interface IObservableValueFactory {
151154
<T>(value?: T, options?: CreateObservableOptions): IObservableValue<T | undefined>
152155
}
153156

157+
export interface IObservableMapFactory {
158+
<K = any, V = any>(): ObservableMap<K, V>
159+
<K, V>(initialValues?: IMapEntries<K, V>, options?: CreateObservableOptions): ObservableMap<
160+
K,
161+
V
162+
>
163+
<K, V>(
164+
initialValues?: IReadonlyMapEntries<K, V>,
165+
options?: CreateObservableOptions
166+
): ObservableMap<K, V>
167+
<K, V>(initialValues?: IKeyValueMap<V>, options?: CreateObservableOptions): ObservableMap<K, V>
168+
<K, V>(initialValues?: Map<K, V>, options?: CreateObservableOptions): ObservableMap<K, V>
169+
<K = any, V = any>(initialValues: undefined, options?: CreateObservableOptions): ObservableMap<
170+
K,
171+
V
172+
>
173+
}
174+
154175
export interface IObservableFactory
155176
extends Annotation,
156177
PropertyDecorator,
@@ -172,10 +193,7 @@ export interface IObservableFactory
172193
initialValues?: IObservableSetInitialValues<T>,
173194
options?: CreateObservableOptions
174195
) => ObservableSet<T>
175-
map: <K = any, V = any>(
176-
initialValues?: IObservableMapInitialValues<K, V>,
177-
options?: CreateObservableOptions
178-
) => ObservableMap<K, V>
196+
map: IObservableMapFactory
179197
object: <T = any>(
180198
props: T,
181199
decorators?: AnnotationsMap<T, never>,

packages/mobx/src/types/observablemap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface IKeyValueMap<V = any> {
4646
export type IMapEntry<K = any, V = any> = [K, V]
4747
export type IReadonlyMapEntry<K = any, V = any> = readonly [K, V]
4848
export type IMapEntries<K = any, V = any> = IMapEntry<K, V>[]
49-
export type IReadonlyMapEntries<K = any, V = any> = IReadonlyMapEntry<K, V>[]
49+
export type IReadonlyMapEntries<K = any, V = any> = readonly IReadonlyMapEntry<K, V>[]
5050

5151
export type IMapDidChange<K = any, V = any> = { observableKind: "map"; debugObjectName: string } & (
5252
| {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13778,10 +13778,10 @@ typedarray@^0.0.6:
1377813778
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
1377913779
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==
1378013780

13781-
typescript@^3.7.3, typescript@^5.6.2:
13782-
version "5.6.2"
13783-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.2.tgz#d1de67b6bef77c41823f822df8f0b3bcff60a5a0"
13784-
integrity sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==
13781+
typescript@^3.7.3, typescript@^5.9.2:
13782+
version "5.9.2"
13783+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.9.2.tgz#d93450cddec5154a2d5cabe3b8102b83316fb2a6"
13784+
integrity sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A==
1378513785

1378613786
uglify-js@^3.1.4:
1378713787
version "3.17.4"

0 commit comments

Comments
 (0)