DEV Community

Discussion on: State manager of dream

Collapse
 
lishine profile image
Pavel Ravits

Seems interesting.
How to use it with typescript, is there an example?

Collapse
 
betula profile image
Slava Birch • Edited

Thanks for your interest!
Totally supported typescript. Most types are calculated automatically. Only several places where are you need to define it manually.

When you define a parameterized action. (play on codesandbox)

// you should pass user type manually for an argument of action
const userUpdate = signal<string>();

// store type will be received from the initial state type
const store = value({ user: "" });

// all arguments will be typed automatically
store.update.by(userUpdate, (state, user) => ({
  ...state,
  user
}))

// only a string type allowed for an argument of action
userUpdate("John");
Enter fullscreen mode Exit fullscreen mode

Or in the shortcut for the above example. (play on codesandbox)

// you should pass user type manually for the second argument
const userUpdate = store.updater((state, user: string) => ({
  ...state,
  user
}));
Enter fullscreen mode Exit fullscreen mode

Types are calculated automatically as more as it possible 👍