From d6fa9a1970ebfb8a8adaf5bf0dc73125acec2dee Mon Sep 17 00:00:00 2001 From: urugator <11457665+urugator@users.noreply.github.com> Date: Sat, 19 Feb 2022 14:06:23 +0100 Subject: [PATCH] fix (#3306) --- .changeset/chilled-cups-raise.md | 5 + .../__tests__/v5/base/typescript-tests.ts | 141 +++++++++++------- packages/mobx/src/api/intercept.ts | 2 +- packages/mobx/src/api/observe.ts | 2 +- 4 files changed, 95 insertions(+), 55 deletions(-) create mode 100644 .changeset/chilled-cups-raise.md diff --git a/.changeset/chilled-cups-raise.md b/.changeset/chilled-cups-raise.md new file mode 100644 index 000000000..5fa45eb9d --- /dev/null +++ b/.changeset/chilled-cups-raise.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +fix missing type inferrence of `observe` and `intercept` for arrays diff --git a/packages/mobx/__tests__/v5/base/typescript-tests.ts b/packages/mobx/__tests__/v5/base/typescript-tests.ts index 6860bee99..2ce5ef056 100644 --- a/packages/mobx/__tests__/v5/base/typescript-tests.ts +++ b/packages/mobx/__tests__/v5/base/typescript-tests.ts @@ -7,10 +7,11 @@ import { autorun, extendObservable, action, - IObservableArray, IArrayDidChange, IArrayWillChange, IArrayWillSplice, + IMapWillChange, + ISetWillChange, IObservableValue, isObservable, isObservableProp, @@ -28,9 +29,9 @@ import { IMapDidChange, IValueDidChange, ISetDidChange, - ISetWillChange, IValueWillChange, - flowResult + flowResult, + IObjectWillChange } from "../../../src/mobx" import * as mobx from "../../../src/mobx" import { assert, IsExact } from "conditional-type-checks" @@ -2054,80 +2055,114 @@ test("type inference of the action callback", () => { }) test("TS - type inference of observe & intercept functions", () => { - const store = observable({ - map: new Map([["testKey", 1]]), - regularObject: { numberKey: 1, stringKey: "string" }, - set: new Set() + const array = [1, 2] + const object = { numberKey: 1, stringKey: "string" } + const map = new Map([["testKey", 1]]) + const set = new Set([1]) + + const { regularArray, regularObject, regularMap, regularSet } = observable({ + regularArray: array, + regularObject: object, + regularMap: map, + regularSet: set }) - const mapMobxFactory = observable.map([["testKey", 1]]) - const setMobxFactory = observable.set() - // Observe regular object - mobx.observe(store.regularObject, argument => { + const observableArray = observable(array) + const observableObject = observable(object) + const observableMap = observable(map) + const observableSet = observable(set) + + // Array + mobx.observe(regularArray, argument => { + assert>>(true) + }) + mobx.intercept(regularArray, argument => { + assert | IArrayWillSplice>>(true) + return argument + }) + // ObservableArray + mobx.observe(observableArray, argument => { + assert>>(true) + }) + mobx.intercept(observableArray, argument => { + assert | IArrayWillSplice>>(true) + return argument + }) + // Object + mobx.observe(regularObject, argument => { assert>(true) }) - - // Observe regular object key - mobx.observe(store.regularObject, "numberKey", argument => { + mobx.intercept(regularObject, argument => { + assert>(true) + return argument + }) + mobx.observe(regularObject, "numberKey", argument => { assert>>(true) }) - - // Intercept regular object key - mobx.intercept(store.regularObject, "numberKey", argument => { + mobx.intercept(regularObject, "numberKey", argument => { assert>>(true) - return null + return argument }) - - // Observe regular map - mobx.observe(store.map, argument => { + // ObservableObject + mobx.observe(observableObject, argument => { + assert>(true) + }) + mobx.intercept(observableObject, argument => { + assert>(true) + return argument + }) + mobx.observe(observableObject, "numberKey", argument => { + assert>>(true) + }) + mobx.intercept(observableObject, "numberKey", argument => { + assert>>(true) + return argument + }) + // Map + mobx.observe(regularMap, argument => { assert>>(true) }) - - // Observe regular map key - mobx.observe(store.map, "testKey", argument => { + mobx.intercept(regularMap, argument => { + assert>>(true) + return argument + }) + mobx.observe(regularMap, "testKey", argument => { assert>>(true) }) - - // Intercept regular map key - mobx.intercept(store.map, "testKey", argument => { + mobx.intercept(regularMap, "testKey", argument => { assert>>(true) - return null + return argument }) - - // Observe map created with observable.map - mobx.observe(mapMobxFactory, argument => { + // ObservableMap + mobx.observe(observableMap, argument => { assert>>(true) }) - - // Observe map key created with observable.map - mobx.observe(mapMobxFactory, "testKey", argument => { + mobx.intercept(observableMap, argument => { + assert>>(true) + return argument + }) + mobx.observe(observableMap, "testKey", argument => { assert>>(true) }) - - // Observe regular set - mobx.observe(store.set, argument => { + mobx.intercept(observableMap, "testKey", argument => { + assert>>(true) + return argument + }) + // Set + mobx.observe(regularSet, argument => { assert>>(true) }) - - // Intercept regular set - mobx.intercept(store.set, argument => { + mobx.intercept(regularSet, argument => { assert>>(true) - return null + return argument }) - - // Observe set created with observable.set - mobx.observe(setMobxFactory, argument => { + // ObservableSet + mobx.observe(observableSet, argument => { assert>>(true) }) - - const ar: IObservableArray = observable([1, 2]) - mobx.observe(ar, d => { - assert>>(true) - }) - - mobx.intercept(ar, c => { - assert | IArrayWillSplice>>(true) - return null + mobx.intercept(observableSet, argument => { + assert>>(true) + return argument }) }) diff --git a/packages/mobx/src/api/intercept.ts b/packages/mobx/src/api/intercept.ts index d72f4604e..9ccb544e1 100644 --- a/packages/mobx/src/api/intercept.ts +++ b/packages/mobx/src/api/intercept.ts @@ -20,7 +20,7 @@ export function intercept( handler: IInterceptor> ): Lambda export function intercept( - observableArray: IObservableArray, + observableArray: IObservableArray | Array, handler: IInterceptor | IArrayWillSplice> ): Lambda export function intercept( diff --git a/packages/mobx/src/api/observe.ts b/packages/mobx/src/api/observe.ts index 13656c80d..f3c4c7c5b 100644 --- a/packages/mobx/src/api/observe.ts +++ b/packages/mobx/src/api/observe.ts @@ -20,7 +20,7 @@ export function observe( fireImmediately?: boolean ): Lambda export function observe( - observableArray: IObservableArray, + observableArray: IObservableArray | Array, listener: (change: IArrayDidChange) => void, fireImmediately?: boolean ): Lambda