Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/fix-snapshot-type-5480.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'xstate': patch
'@xstate/react': patch
---

Fix `ActorOptions.snapshot` and `Actor.getPersistedSnapshot()` to return `SnapshotFrom<TLogic>` instead of `Snapshot<unknown>`, so that rehydrating a machine from a persisted snapshot no longer produces TypeScript errors.

```ts
const persisted = actor.getPersistedSnapshot(); // typed SnapshotFrom<TMachine>
useMachine(machine, { snapshot: persisted }); // now typechecks
```

Resolves [#5480](https://github.com/statelyai/xstate/issues/5480).
9 changes: 6 additions & 3 deletions packages/core/src/createActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,12 @@ export class Actor<TLogic extends AnyActorLogic>
* Can be restored with {@link ActorOptions.state}
* @see https://stately.ai/docs/persistence
*/
public getPersistedSnapshot(): Snapshot<unknown>;
public getPersistedSnapshot(options?: unknown): Snapshot<unknown> {
return this.logic.getPersistedSnapshot(this._snapshot, options);
public getPersistedSnapshot(): SnapshotFrom<TLogic>;
public getPersistedSnapshot(options?: unknown): SnapshotFrom<TLogic> {
return this.logic.getPersistedSnapshot(
this._snapshot,
options
) as SnapshotFrom<TLogic>;
}

public [symbolObservable](): InteropSubscribable<SnapshotFrom<TLogic>> {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1865,10 +1865,10 @@ export interface ActorOptions<TLogic extends AnyActorLogic> {
* Can be generated with {@link Actor.getPersistedSnapshot}.
* @see https://stately.ai/docs/persistence
*/
snapshot?: Snapshot<unknown>;
snapshot?: SnapshotFrom<TLogic>;

/** @deprecated Use `snapshot` instead. */
state?: Snapshot<unknown>;
state?: SnapshotFrom<TLogic>;

/** The source actor logic. */
src?: string | AnyActorLogic;
Expand Down
3 changes: 2 additions & 1 deletion packages/xstate-react/test/createActorContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assign,
fromPromise,
Snapshot,
SnapshotFrom,
InspectionEvent
} from 'xstate';
import { fireEvent, screen, render, waitFor } from '@testing-library/react';
Expand Down Expand Up @@ -374,7 +375,7 @@ describe('createActorContext', () => {
}
});
const SomeContext = createActorContext(machine);
let persistedState: Snapshot<unknown> | undefined = undefined;
let persistedState: SnapshotFrom<typeof machine> | undefined = undefined;

const Component = () => {
const actorRef = SomeContext.useActorRef();
Expand Down
34 changes: 33 additions & 1 deletion packages/xstate-react/test/types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { render } from '@testing-library/react';
import { ActorRefFrom, assign, createMachine, setup } from 'xstate';
import {
ActorRefFrom,
assign,
createActor,
createMachine,
setup
} from 'xstate';
import {
useActor,
useActorRef,
Expand Down Expand Up @@ -218,3 +224,29 @@ it('useMachine types work for machines with a specified id and state with an aft
return state.matches('enabled');
}
});

// https://github.com/statelyai/xstate/issues/5480
it('useMachine accepts a rehydrated snapshot from getPersistedSnapshot #5480', () => {
const counterMachine = setup({}).createMachine({
initial: 'idle',
states: {
idle: {},
active: {}
}
});

// Round-trip: getPersistedSnapshot() -> useMachine(machine, { snapshot })
// Both sides must typecheck without `as any` casts or @ts-expect-error.
function App() {
const actorRef = createActor(counterMachine).start();
const persistedState = actorRef.getPersistedSnapshot();

const [, , restoredRef] = useMachine(counterMachine, {
snapshot: persistedState
});

return <span data-testid="value">{restoredRef.getSnapshot().value}</span>;
}

render(<App />);
});
3 changes: 2 additions & 1 deletion packages/xstate-react/test/useActor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ActorRef,
DoneActorEvent,
Snapshot,
SnapshotFrom,
StateFrom,
assign,
createActor,
Expand Down Expand Up @@ -86,7 +87,7 @@ describeEachReactMode('useActor (%s)', ({ suiteKey, render }) => {

const Fetcher: React.FC<{
onFetch: () => Promise<any>;
persistedState?: Snapshot<unknown>;
persistedState?: SnapshotFrom<typeof fetchMachine>;
}> = ({
onFetch = () => {
return new Promise((res) => res('some data'));
Expand Down