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] Add schema #1977

Merged
merged 6 commits into from
Mar 19, 2021
Merged

[core] Add schema #1977

merged 6 commits into from
Mar 19, 2021

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Mar 11, 2021

This PR adds the schema property to the machine config and definition.

const machine = createMachine({
  // ...
  schema: {
    // These can be arbitrary meta data, accessible at runtime
    // Typecast these (something as SpecificType) for inferred types!
    context: someContextSchema,
    events: someEventSchemaMap,
    actions: someActionSchemaMap,
    guards: someGuardSchemaMap,
    services: someServiceSchemaMap
  },
  // ...
});

Purpose of this PR:

  • Provides a place to specify important metadata for runtime types and validation (e.g., with JSON Schema) as part of the machine definition
  • Allows for easier type inference without having to specify generics. Notice how the context and events are strongly typed, even though their generic types aren't specified:

Screen Shot 2021-03-10 at 10 10 00 PM

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2021

🦋 Changeset detected

Latest commit: 907d577

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

This PR includes changesets to release 3 packages
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

.changeset/chilled-windows-destroy.md Outdated Show resolved Hide resolved
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
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.

None yet

2 participants