github mobxjs/mobx-state-tree v6.0.0

latest releases: v7.0.0-pre.2, v7.0.0-pre.1, v6.0.1...
6 months ago

Breaking Changes

Features

  • Added a new feature, hasEnv, to support #2163

Fixes

  • Union types will now be fixed, but this also may break TypeScript for some users
  • Array operations produce condensed patches when we can, but this may break for some users who relied on the patch generation as it was
  • fix: do not mutate properties object by model constructor by @dangreen in #2176
  • Fix/get members 2173 by @evinosheaforward in #2174
  • fix: #2138 linter issues with fail function by @JuanJo4 in #2171

Tests

Docs

Community/Developer Changes

New Contributors

Full Changelog: v5.4.2...v6.0.0

Migration Guide from v5 to v6

TypeScript update

Make sure you're using TypeScript 5.3.3, and you have the following compiler flags:

{
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true
}

Or the shorthand:

{
  "strict": true,
  "noImplicitReturns": true
}

Removing $nonEmptyObject

We removed the $nonEmptyObject key from the ModelCreationType. For the most part, this was an internal feature that would only have shown up in TypeScript errors. There is no runtime change, but if you have any custom typings that relied on something like [$nonEmptyObject] - you should be able to eliminate those.

Union Type Changes

In the past, named enumerations had more specific type than unnamed enumerations. You may have some TypeScript errors where you were using anonymous enumerations. You can type those with the union of literal values inside the enumeration now.

import { t } from "mobx-state-tree";

/**
 * In MobX-State-Tree 5.4.1, this is typed as:
 * ISimpleType<"Red" | "Orange" | "Green">
 */
const namedEnum = t.enumeration("Color", ["Red", "Orange", "Green"]);

/**
 * In MobX-State-Tree 5.4.1, this is typed as:
 * ISimpleType<string>
 */
const anonymousEnum = t.enumeration(["Red", "Orange", "Green"]);

/**
 * If you use mobx-state-tree@^6.0.0, both of these will be typed as:
 * ISimpleType<"Red" | "Orange" | "Green">
 */

This has also fixed #1525 and #1664

For #1525, If you had models with properties that were unions including other models, you may have done some type assertions to resolve the issue. This should no longer be necessary. In most cases, we don't expect this to show up as errors. But the inferred types have changed. Look through your codebase for unions of models in the properties of other models, and if you hit any issues, you may be able to use better typing form here on out.

The issue presented itself in #1664 when using types with different creation and instance types which were passed to unions inside of arrays. If you see more TypeScript errors aside from the other items mentioned, look for types.union that creates a union which includes Array, Map, or Model types. If you had any custom type assertions, you may no longer need those.

Array operation patch changes

When an Array type calls clear, or splices itself from index 0, the generated patch is different. We have shortened the operations to be a patch like:

op: "replace", path: "", value: []

If you use onPatch and expect any specific shape of patches from Array.clear or Array.splice, you may need to update your app logic to handle the changed patch generation.

getEnv now throws

If you call getEnv and there is no environment, it will now throw an error. Previously, it would return an empty object.

Check your codebase for getEnv calls, and guard against this error with hasEnv, which will return true if the current state tree has an environment, or false if not.

Don't miss a new mobx-state-tree release

NewReleases is sending notifications on new releases.