@doeixd/machine 0.0.17 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -165,11 +165,11 @@ The most powerful pattern: different machine types represent different states.
165
165
  import { createMachine, Machine } from "@doeixd/machine";
166
166
 
167
167
  // Define distinct machine types for each state
168
- type LoggedOut = Machine<{ status: "loggedOut" }> & {
168
+ type LoggedOut = Machine<{ status: "loggedOut" }, {
169
169
  login: (username: string) => LoggedIn;
170
170
  };
171
171
 
172
- type LoggedIn = Machine<{ status: "loggedIn"; username: string }> & {
172
+ type LoggedIn = Machine<{ status: "loggedIn"; username: string }, {
173
173
  logout: () => LoggedOut;
174
174
  viewProfile: () => LoggedIn;
175
175
  };
@@ -236,12 +236,12 @@ logout(state); // Runtime error!
236
236
  **Type-State Approach (Compile-Time Enforcement):**
237
237
  ```typescript
238
238
  // ✅ States are distinct types - compiler enforces validity
239
- type LoggedOut = Machine<{ status: "loggedOut" }> & {
239
+ type LoggedOut = Machine<{ status: "loggedOut" }, {
240
240
  login: (user: string) => LoggedIn;
241
241
  // No logout method - impossible to call
242
242
  };
243
243
 
244
- type LoggedIn = Machine<{ status: "loggedIn"; username: string }> & {
244
+ type LoggedIn = Machine<{ status: "loggedIn"; username: string }, {
245
245
  logout: () => LoggedOut;
246
246
  // No login method - impossible to call
247
247
  };
@@ -308,7 +308,7 @@ if (hasState(machine, "status", "success")) {
308
308
 
309
309
  #### 5. Event Type Safety
310
310
  ```typescript
311
- type FetchMachine = AsyncMachine<{ status: string }> & {
311
+ type FetchMachine = AsyncMachine<{ status: string }, {
312
312
  fetch: (id: number) => Promise<FetchMachine>;
313
313
  retry: () => Promise<FetchMachine>;
314
314
  };
@@ -370,22 +370,22 @@ This shows the full power of Type-State Programming:
370
370
 
371
371
  ```typescript
372
372
  // Define the states as distinct types
373
- type IdleState = Machine<{ status: "idle" }> & {
373
+ type IdleState = Machine<{ status: "idle" }, {
374
374
  fetch: (url: string) => LoadingState;
375
375
  };
376
376
 
377
- type LoadingState = Machine<{ status: "loading"; url: string }> & {
377
+ type LoadingState = Machine<{ status: "loading"; url: string }, {
378
378
  cancel: () => IdleState;
379
379
  // Note: No fetch - can't start new request while loading
380
380
  };
381
381
 
382
- type SuccessState = Machine<{ status: "success"; data: any }> & {
382
+ type SuccessState = Machine<{ status: "success"; data: any }, {
383
383
  refetch: () => LoadingState;
384
384
  clear: () => IdleState;
385
385
  // Note: No cancel - nothing to cancel
386
386
  };
387
387
 
388
- type ErrorState = Machine<{ status: "error"; error: string }> & {
388
+ type ErrorState = Machine<{ status: "error"; error: string }, {
389
389
  retry: () => LoadingState;
390
390
  clear: () => IdleState;
391
391
  };
@@ -779,7 +779,7 @@ const tracked = withAnalytics(machine, trackEvent);
779
779
  ```typescript
780
780
  import { Context, Transitions, Event, TransitionArgs } from "@doeixd/machine";
781
781
 
782
- type MyMachine = Machine<{ count: number }> & {
782
+ type MyMachine = Machine<{ count: number }, {
783
783
  add: (n: number) => MyMachine;
784
784
  };
785
785