question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Less awkward way to pass context and event types

See original GitHub issue

useStateMachine is a curried function (Yummm tasty!) because TypeScript doesn’t yet support partial generics type inference. This workaround allows TypeScript developers to provide a custom type for the context while still having TypeScript infer all the types used in the configuration (Like the state & transitions names, etc…).

We no longer have this limitation with #36. We can have something like the following for context…

useStateMachine({
  initial: "idle",
  context: { foo: 1 } // I prefer "initialContext" but whatever xD
  ...
})

And something like this (what xstate v5 does), for events and context…

useStateMachine({
  schema: {
    context: createSchema<{ foo: number, bar?: number }>(),
    event: createSchema<
      | { type: "X", foo: number }  
      | { type: "Y", bar: number }
    >(),
    // I prefer "event" instead of "events" because the type is for event and not "events"
    // (unions need to be named singular even though they seem plural)
    // though "events" works too if that's more friendly
  },
  initial: "idle",
  context: { foo: 1 }
  ...
})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
devanshjcommented, Jul 9, 2021

Cool so I’ll update the PR soon and it’ll be good to merge!

1reaction
cassiozencommented, Jun 18, 2021

Oh, that looks pretty good. Let’s go with it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

7 Tricky Work Situations, and How to Respond to Them
Situation #5: You have to give negative or awkward feedback to someone you're close with. Tony is a purchaser at a chocolate factory....
Read more >
Managing Difficult Classroom Discussions
This page provides tips and resources for managing heated or contentious conversations in the classroom.
Read more >
12 Interactive Presentation Ideas Your Event Audience Will Love
Try these Interactive Presentation Ideas for Amazing Events: ... It's less awkward – When people are already moving in ways that don't feel...
Read more >
AWS Lambda function handler in TypeScript
The AWS Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the...
Read more >
Examples Of Various Ways To Invite People To Hang Out
This article gives a variety of examples of different ways someone might ask a new potential friend to do something with them.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found