Skip to content

Commit 20c2215

Browse files
elyor-shkubk
andauthored
fix(types): accept requiresObservable in when options (#4746)
* fix(types): accept requiresObservable in when options * test: convert autorunAsync to TS so when options are typechecked Replaces the added runtime test with type coverage from the existing requiresObservable tests, and drops the stale 'Experimental' comments. --------- Co-authored-by: Gorbachev Egor <7gorbachevm@gmail.com>
1 parent bd8f02d commit 20c2215

4 files changed

Lines changed: 15 additions & 17 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx": patch
3+
---
4+
5+
Add `requiresObservable` to `IWhenOptions`. `when` already forwarded this option to its underlying reaction at runtime, but TypeScript rejected it.

packages/mobx/__tests__/base/autorunAsync.js renamed to packages/mobx/__tests__/base/autorunAsync.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
/**
2-
* @type {typeof import("../../src/mobx")}
3-
*/
4-
const mobx = require("../../src/mobx.ts")
5-
6-
const utils = require("../utils/test-utils")
1+
import * as mobx from "../../src/mobx"
2+
import * as utils from "../utils/test-utils"
73

84
const { $mobx } = mobx
95

106
test("autorun 1", function (done) {
117
let _fired = 0
12-
let _result = null
8+
let _result: number | null = null
139
let _cCalcs = 0
1410
const to = setTimeout
1511

16-
function check(fired, cCalcs, result) {
12+
function check(fired: number, cCalcs: number, result: number | null) {
1713
expect(_fired).toBe(fired)
1814
expect(_cCalcs).toBe(cCalcs)
1915
if (fired) expect(_result).toBe(result)
@@ -113,7 +109,7 @@ test("autorun should not result in loop", function (done) {
113109
})
114110

115111
test("autorunAsync passes Reaction as an argument to view function", function (done) {
116-
const a = mobx.observable.box(1)
112+
const a = mobx.observable.box<number | string>(1)
117113

118114
let autoRunsCalled = 0
119115

@@ -186,7 +182,7 @@ test("reaction accepts a scheduling function", function (done) {
186182
let schedulingsCalled = 0
187183
let exprCalled = 0
188184

189-
const values = []
185+
const values: number[] = []
190186

191187
mobx.reaction(
192188
() => {
@@ -283,7 +279,7 @@ test("whenWithTimeout should dispose", done => {
283279

284280
const d2 = mobx.when(
285281
() => a.get() === 2,
286-
() => t.fail("2 should not finsih"),
282+
() => done.fail("2 should not finsih"),
287283
{
288284
timeout: 200,
289285
onError: () => done.fail("2 should not timeout")
@@ -305,7 +301,7 @@ test("whenWithTimeout should dispose", done => {
305301
describe("when opts requiresObservable", () => {
306302
test("warn when no observable", () => {
307303
utils.consoleWarn(() => {
308-
const disposer = mobx.when(() => 2, {
304+
const disposer = mobx.when(() => true, {
309305
requiresObservable: true
310306
})
311307

@@ -319,7 +315,7 @@ describe("when opts requiresObservable", () => {
319315
})
320316

321317
const messages = utils.supressConsole(() => {
322-
const disposer = mobx.when(() => obsr.x, {
318+
const disposer = mobx.when(() => obsr.x === 1, {
323319
requiresObservable: true
324320
})
325321

‎packages/mobx/src/api/autorun.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ import {
1919
export interface IAutorunOptions {
2020
delay?: number
2121
name?: string
22-
/**
23-
* Experimental.
24-
* Warns if the view doesn't track observables
25-
*/
2622
requiresObservable?: boolean
2723
scheduler?: (callback: () => void) => any
2824
onError?: (error: any) => void

‎packages/mobx/src/api/when.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface IWhenOptions {
1616
timeout?: number
1717
onError?: (error: any) => void
1818
signal?: GenericAbortSignal
19+
requiresObservable?: boolean
1920
}
2021

2122
export function when(

0 commit comments

Comments
 (0)