Skip to content

Commit

Permalink
fix(type): remove proxy option for makeObservable and makeAutoObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
liucan233 committed Jul 6, 2023
1 parent 27efa3c commit fb4928f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-ducks-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

remove proxy option for makeObservable and makeAutoObservable
16 changes: 15 additions & 1 deletion docs/observable-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ Inference rules:
- All _generator_ functions become `flow`. (Note that generator functions are not detectable in some transpiler configurations, if flow doesn't work as expected, make sure to specify `flow` explicitly.)
- Members marked with `false` in the `overrides` argument will not be annotated. For example, using it for read only fields such as identifiers.

## Options {🚀}

The above APIs take an optional `options` argument which is an object that supports the following options:

- **`autoBind: true`** uses `action.bound`/`flow.bound` by default, rather than `action`/`flow`. Does not affect explicitely annotated members.
- **`deep: false`** uses `observable.ref` by default, rather than `observable`. Does not affect explicitely annotated members.
- **`name: <string>`** gives the object a debug name that is printed in error messages and reflection APIs.

<details id="one-options-per-target"><summary>**Note:** options are *sticky* and can be provided only once<a href="#one-options-per-target" class="tip-anchor"></a></summary>
`options` argument can be provided only for `target` that is NOT observable yet.<br>
It is NOT possible to change options once the observable object was initialized.<br>
Options are stored on target and respected by subsequent `makeObservable`/`extendObservable` calls.<br>
You can't pass different options in [subclass](subclassing.md).
</details>
## `observable`

Usage:
Expand Down Expand Up @@ -255,7 +269,7 @@ Note that it is possible to pass `{ proxy: false }` as an option to `observable`

## Options {🚀}

The above APIs take an optional `options` argument which is an object that supports the following options:
The `observable` take an optional `options` argument which is an object that supports the following options:

- **`autoBind: true`** uses `action.bound`/`flow.bound` by default, rather than `action`/`flow`. Does not affect explicitely annotated members.
- **`deep: false`** uses `observable.ref` by default, rather than `observable`. Does not affect explicitely annotated members.
Expand Down
11 changes: 9 additions & 2 deletions packages/mobx/src/api/makeObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ import {
// Fixes: https://github.com/mobxjs/mobx/issues/2325#issuecomment-691070022
type NoInfer<T> = [T][T extends any ? 0 : never]

// Options property "proxy" designs for "observable" function,
// and makeObservable and makeAutoObservable is not Supported
// const foo = observable({}), foo.bar = 1 will be monitored,
// const foo = observable({}, {proxy: false}), foo.bar = 1 will be not monitored
// The summary is that proxy is an invalid option for makeObservable and makeAutoObservable
type NoProxyCreateObservableOptions = Omit<CreateObservableOptions, "proxy">

export function makeObservable<T extends object, AdditionalKeys extends PropertyKey = never>(
target: T,
annotations?: AnnotationsMap<T, NoInfer<AdditionalKeys>>,
options?: CreateObservableOptions
options?: NoProxyCreateObservableOptions
): T {
const adm: ObservableObjectAdministration = asObservableObject(target, options)[$mobx]
startBatch()
Expand All @@ -53,7 +60,7 @@ const keysSymbol = Symbol("mobx-keys")
export function makeAutoObservable<T extends object, AdditionalKeys extends PropertyKey = never>(
target: T,
overrides?: AnnotationsMap<T, NoInfer<AdditionalKeys>>,
options?: CreateObservableOptions
options?: NoProxyCreateObservableOptions
): T {
if (__DEV__) {
if (!isPlainObject(target) && !isPlainObject(Object.getPrototypeOf(target))) {
Expand Down

0 comments on commit fb4928f

Please # to comment.