MyGit

v6.0.0

mobxjs/mobx-state-tree

版本发布时间: 2024-05-07 03:16:58

mobxjs/mobx-state-tree最新发布版本:v6.0.0(2024-05-07 03:16:58)

Breaking Changes

Features

Fixes

Tests

Docs

Community/Developer Changes

New Contributors

Full Changelog: https://github.com/mobxjs/mobx-state-tree/compare/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 https://github.com/mobxjs/mobx-state-tree/issues/1525 and https://github.com/mobxjs/mobx-state-tree/issues/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.

相关地址:原始地址 下载(tar) 下载(zip)

查看:2024-05-07发行的版本