Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Merge actions and guards schema maps in extend()#5594

Merged
davidkpiano merged 1 commit into
statelyai:nextfrom
asfktz:extend-merge-actions-guards
Jul 5, 2026
Merged

Merge actions and guards schema maps in extend()#5594
davidkpiano merged 1 commit into
statelyai:nextfrom
asfktz:extend-merge-actions-guards

Conversation

@asfktz

@asfktz asfktz commented Jul 5, 2026

Copy link
Copy Markdown

Previously, extend() merged schema maps for events, emitted, and children, but replaced actions and guards entirely. As a result, extending with new action or guard schemas would discard the base ones.

Now, actions and guards are also included so that all five schema fields are shallow-merged.

import { setup, types } from 'xstate';

const baseDef = setup({
  schemas: {
    actions: {
      track: { params: types<{ key: string }>() }
    },
    guards: {
      hasAccess: { params: types<{ role: string }>() }
    }
  }
})


const extendedDef = baseDef.extend({
  schemas: {
    actions: {
      notify: { params: types<{ message: string }>() }
    },
    guards: {
      canReset: { params: types<{ reason: string }>() }
    }
  }
})

// Both base and extended actions/guards are available
const machine = extendedDef.createMachine({
  entry: ({ actions }, enq) => {
    enq(actions.track({ key: 'init' }));
    enq(actions.notify({ message: 'started' }));
  },
  on: {
    RESET: ({ guards }) => {
      if (
        guards.hasAccess({ role: 'admin' }) &&
        guards.canReset({ reason: 'manual' })
      ) {
        return { target: '.idle' };
      }
    }
  }
});

@changeset-bot

changeset-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c064450

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@asfktz asfktz force-pushed the extend-merge-actions-guards branch from f4e971d to 4b5dbd3 Compare July 5, 2026 02:03
@davidkpiano

Copy link
Copy Markdown
Member

Merged in 5595; can you fix merge conflicts?

@asfktz asfktz force-pushed the extend-merge-actions-guards branch from 4b5dbd3 to c064450 Compare July 5, 2026 02:18
@asfktz

asfktz commented Jul 5, 2026

Copy link
Copy Markdown
Author

Merged in 5595; can you fix merge conflicts?

@davidkpiano done

@davidkpiano davidkpiano merged commit 95a7aca into statelyai:next Jul 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants