Skip to content

Commit

Permalink
Fixed inference for assign using PropertyAssigner (#3818)
Browse files Browse the repository at this point in the history
* Fixed inference for `assign` using `PropertyAssigner`

* fixed types in tests

* Deduplicate TS version

* Create tough-lizards-begin.md
  • Loading branch information
Andarist committed Feb 9, 2023
1 parent 5108c25 commit 2d8d84f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .changeset/tough-lizards-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"xstate": patch
---

Fixed inference for `assign` using `PropertyAssigner`, like here:
```ts
actions: assign({
counter: 0,
delta: (ctx, ev) => ev.delta,
})
```
7 changes: 5 additions & 2 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import {
EventFrom,
AnyActorRef,
PredictableActionArgumentsExec,
BaseActionObject
BaseActionObject,
LowInfer
} from './types';
import * as actionTypes from './actionTypes';
import {
Expand Down Expand Up @@ -467,7 +468,9 @@ export function resolveStop<TContext, TEvent extends EventObject>(
* @param assignment An object that represents the partial context to update.
*/
export const assign = <TContext, TEvent extends EventObject = EventObject>(
assignment: Assigner<TContext, TEvent> | PropertyAssigner<TContext, TEvent>
assignment:
| Assigner<LowInfer<TContext>, TEvent>
| PropertyAssigner<LowInfer<TContext>, TEvent>
): AssignAction<TContext, TEvent> => {
return {
type: actionTypes.assign,
Expand Down
41 changes: 41 additions & 0 deletions packages/core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,4 +889,45 @@ describe('actions', () => {
]
});
});

it('should accept assign with partial static object', () => {
createMachine({
schema: {
events: {} as {
type: 'TOGGLE';
},
context: {} as {
count: number;
mode: 'foo' | 'bar' | null;
}
},
context: {
count: 0,
mode: null
},
entry: assign({ mode: 'foo' })
});
});

it("should provide context to single prop updater in assign when it's mixed with a static value for another prop", () => {
createMachine({
schema: {
context: {} as {
count: number;
skip: boolean;
},
events: {} as {
type: 'TOGGLE';
}
},
context: {
count: 0,
skip: true
},
entry: assign({
count: (context) => context.count + 1,
skip: true
})
});
});
});
13 changes: 7 additions & 6 deletions packages/xstate-react/test/useSelector.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { act, fireEvent, screen } from '@testing-library/react';
import * as React from 'react';
import {
ActorRef,
ActorRefFrom,
AnyState,
assign,
Expand Down Expand Up @@ -294,7 +295,7 @@ describeEachReactMode('useSelector (%s)', ({ suiteKey, render }) => {
const parentMachine = createMachine({
schema: {
context: {} as {
childActor: ActorRefFrom<ReturnType<typeof createActor>>;
childActor: ActorRef<any, any>;
}
},
entry: assign({
Expand All @@ -303,8 +304,8 @@ describeEachReactMode('useSelector (%s)', ({ suiteKey, render }) => {
});

const identitySelector = (value: any) => value;
const getSnapshot = (actor: ReturnType<typeof createActor>) =>
actor.latestValue;
const getSnapshot = (actor: ActorRef<any, any>) =>
(actor as any).latestValue;

const App = () => {
const [state] = useMachine(parentMachine);
Expand Down Expand Up @@ -433,7 +434,7 @@ describeEachReactMode('useSelector (%s)', ({ suiteKey, render }) => {
const parentMachine = createMachine({
schema: {
context: {} as {
childActor: ActorRefFrom<ReturnType<typeof createActor>>;
childActor: ActorRef<any, any>;
}
},
entry: assign({
Expand All @@ -442,8 +443,8 @@ describeEachReactMode('useSelector (%s)', ({ suiteKey, render }) => {
});

const identitySelector = (value: any) => value;
const getSnapshot = (actor: ReturnType<typeof createActor>) =>
actor.latestValue;
const getSnapshot = (actor: ActorRef<any, any>) =>
(actor as any).latestValue;

const App = () => {
const [state] = useMachine(parentMachine);
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9464,12 +9464,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@*:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==

typescript@^4.8.4:
typescript@*, typescript@^4.8.4:
version "4.8.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==
Expand Down

0 comments on commit 2d8d84f

Please sign in to comment.