Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Strongly-typed state value #5006

Merged
merged 11 commits into from
Aug 23, 2024

Conversation

davidkpiano
Copy link
Member

The state value typings for setup state machine actors (setup({}).createMachine({ ... })) have been improved to represent the actual expected state values.

const machine = setup({}).createMachine({
  initial: 'green',
  states: {
    green: {},
    yellow: {},
    red: {
      initial: 'walk',
      states: {
        walk: {},
        wait: {},
        stop: {}
      }
    },
    emergency: {
      type: 'parallel',
      states: {
        main: {
          initial: 'blinking',
          states: {
            blinking: {}
          }
        },
        cross: {
          initial: 'blinking',
          states: {
            blinking: {}
          }
        }
      }
    }
  }
});

const actor = createActor(machine).start();

const stateValue = actor.getSnapshot().value;

if (stateValue === 'green') {
  // ...
} else if (stateValue === 'yellow') {
  // ...
} else if ('red' in stateValue) {
  stateValue;
  // {
  //   red: "walk" | "wait" | "stop";
  // }
} else {
  stateValue;
  // {
  //   emergency: {
  //     main: "blinking";
  //     cross: "blinking";
  //   };
  // }
}

Copy link

changeset-bot bot commented Jul 30, 2024

🦋 Changeset detected

Latest commit: d7e9224

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

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

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

};
};

snapshot.value satisfies ExpectedType;
({}) as ExpectedType satisfies typeof snapshot.value;
});

it('state.value from setup state machine actors should be strongly-typed', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this test before seeing the others, but I like it 🥺

@davidkpiano
Copy link
Member Author

@Andarist Ready for review

@davidkpiano davidkpiano reopened this Aug 20, 2024
@davidkpiano davidkpiano enabled auto-merge (squash) August 23, 2024 14:20
@davidkpiano davidkpiano merged commit 1ab9745 into main Aug 23, 2024
1 check passed
@davidkpiano davidkpiano deleted the davidkpiano/strongly-typed-state-value branch August 23, 2024 14:53
@github-actions github-actions bot mentioned this pull request Aug 23, 2024
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.

1 participant