kossnocorp / superstate

Type-safe JavaScript statecharts library
https://superstate.io
MIT License
85 stars 0 forks source link

Context #2

Closed kossnocorp closed 4 months ago

kossnocorp commented 5 months ago

The goal of this task is to add context type to states, so sending an event would require providing the given context shape. The context will be available on event/state updates and state objects.

The challenge is to make it fully type-safe, so passing a wrong context shape would be impossible.

Currently, the state looks like this:

form.send("submit() -> profile", {
  ref: "hello",
  email: "koss@nocorp.me",
  password: "123456",
});

Since multiple transitions can be triggered by the same name event, specifying the destination state (profile in submit() -> profile) for an event would be required for the types to know which shape to expect.

It is possible to omit the destination in case of unambiguous transitions, but it will create an inconsistent API, so I'm 100% confident it's a good idea.

kossnocorp commented 4 months ago

Shipped with superstate@1.0.0-beta.4