-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
"Durations defined in setup() are not strongly typed " #5399
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
Open
Sambhram1
wants to merge
2
commits into
statelyai:main
Choose a base branch
from
Sambhram1:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Fix: Add runtime warning for unknown delay keys in `after` transitions | ||
|
|
||
| ## Problem | ||
|
|
||
| According to the XState documentation, delays should be typed when defined in `setup()`. However, due to a TypeScript limitation (issue #55709), the compiler cannot properly validate delay keys in `after` transitions when using `setup().createMachine()`. | ||
|
|
||
| Additionally, when an unknown delay key is referenced at runtime, the machine silently treats it as an immediate transition (no delay), which can lead to unexpected behavior that's difficult to debug. | ||
|
|
||
| ## Solution | ||
|
|
||
| ### 1. Runtime Warning (Development Mode) | ||
|
|
||
| Added a development-mode warning in `packages/core/src/actions/raise.ts` that alerts developers when they reference an unknown delay key: | ||
|
|
||
| ```typescript | ||
| if (isDevelopment && configDelay === undefined) { | ||
| console.warn( | ||
| `Delay "${delay}" is not configured in \`delays\`. The event will be raised immediately. ` + | ||
| `This is likely a mistake. Available delays: ${Object.keys(delaysMap || {}).join(', ') || 'none'}.` | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| This warning helps developers catch delay configuration mistakes during development. | ||
Sambhram1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### 2. Test Coverage | ||
|
|
||
| Added comprehensive tests in `packages/core/test/delays.test.ts` that verify: | ||
| - Warning is shown when unknown delay is referenced | ||
| - Warning is shown when no delays are configured | ||
| - No warning for numeric delays | ||
| - No warning for properly configured delays | ||
| - Immediate transition behavior is documented | ||
|
|
||
| ### 3. Documentation | ||
|
|
||
| Updated type test comments in `packages/core/test/setup.types.test.ts` to clarify that the TypeScript limitation is known and that runtime warnings provide the safety net. | ||
|
|
||
| ## TypeScript Limitation | ||
|
|
||
| The `@x-ts-expect-error` annotations in the type tests reference TypeScript issue #55709, which prevents proper narrowing of mapped types with string unions. This is a compiler limitation, not an XState issue. | ||
|
|
||
| However, XState DOES provide type checking when using `createMachine` directly with `types: { delays: ... }`: | ||
|
|
||
| ```typescript | ||
| createMachine({ | ||
| types: {} as { | ||
| delays: 'one second' | 'one minute'; | ||
| }, | ||
| after: { | ||
| // @ts-expect-error - properly caught! | ||
| 'unknown delay': {} | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| Run the new tests: | ||
| ```bash | ||
| pnpm -C packages/core test delays.test.ts | ||
| ``` | ||
|
|
||
| Run all tests: | ||
| ```bash | ||
| pnpm test | ||
| ``` | ||
|
|
||
| ## Breaking Changes | ||
|
|
||
| None. This change only adds development-mode warnings and does not modify runtime behavior or public APIs. | ||
Sambhram1 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Add development-mode warning for unknown delay keys in `after` transitions | ||
|
|
||
| ## Summary | ||
|
|
||
| Fixes the issue where unknown delay keys in `after` transitions are silently treated as immediate transitions, making it difficult to debug configuration mistakes. | ||
|
|
||
| ## Problem | ||
|
|
||
| When using `setup().createMachine()` with delays configured: | ||
|
|
||
| ```typescript | ||
| const machine = setup({ | ||
| delays: { | ||
| slotDuration: 100 | ||
| } | ||
| }).createMachine({ | ||
| states: { | ||
| sleep: { | ||
| after: { | ||
| slotDuration222: 'wake' // Typo! But no error shown | ||
| } | ||
| }, | ||
| wake: {} | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| **Current behavior:** | ||
| 1. TypeScript doesn't catch the typo due to a compiler limitation (TS#55709) | ||
| 2. Runtime silently treats the unknown delay as immediate transition | ||
| 3. Machine transitions instantly instead of waiting, causing hard-to-debug issues | ||
|
|
||
| **Expected behavior:** | ||
| - Developers should be warned about the misconfiguration | ||
| - The issue should be caught during development | ||
|
|
||
| ## Solution | ||
|
|
||
| ### 1. Runtime Warning (Development Mode Only) | ||
|
|
||
| Added a warning in `packages/core/src/actions/raise.ts` that triggers when an unknown delay key is referenced: | ||
|
|
||
| ```typescript | ||
| if (isDevelopment && configDelay === undefined) { | ||
| console.warn( | ||
| `Delay "${delay}" is not configured in \`delays\`. The event will be raised immediately. ` + | ||
| `This is likely a mistake. Available delays: ${Object.keys(delaysMap || {}).join(', ') || 'none'}.` | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| **Example output:** | ||
| ``` | ||
| Delay "slotDuration222" is not configured in `delays`. The event will be raised immediately. This is likely a mistake. Available delays: slotDuration. | ||
| ``` | ||
|
|
||
| ### 2. Comprehensive Test Coverage | ||
|
|
||
| Added `packages/core/test/delays.test.ts` with tests covering: | ||
| - ✅ Warning when unknown delay is referenced | ||
| - ✅ Warning when no delays are configured | ||
| - ✅ No warning for numeric delays | ||
| - ✅ No warning for properly configured delays | ||
| - ✅ Documented immediate transition behavior | ||
|
|
||
| ### 3. Documentation Updates | ||
|
|
||
| Updated type test comments in `packages/core/test/setup.types.test.ts` to clarify: | ||
| - The TypeScript limitation (TS#55709) is known | ||
| - Runtime warnings provide the safety net | ||
| - The issue is a compiler limitation, not an XState bug | ||
|
|
||
| ## TypeScript Limitation Explained | ||
|
|
||
| The `@x-ts-expect-error` annotations in type tests reference [TypeScript issue #55709](https://github.com/microsoft/TypeScript/issues/55709), which prevents proper narrowing of mapped types with string unions when used in `setup().createMachine()`. | ||
|
|
||
| However, XState DOES provide type checking when using `createMachine` directly: | ||
|
|
||
| ```typescript | ||
| createMachine({ | ||
| types: {} as { | ||
| delays: 'one second' | 'one minute'; | ||
| }, | ||
| after: { | ||
| 'unknown delay': {} // @ts-expect-error - properly caught! | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ## Changes | ||
|
|
||
| ### Modified Files | ||
| 1. **packages/core/src/actions/raise.ts** - Added development-mode warning | ||
| 2. **packages/core/test/setup.types.test.ts** - Updated comments to clarify known TypeScript limitation | ||
| 3. **packages/core/test/delays.test.ts** - NEW: Added comprehensive runtime tests | ||
|
|
||
| ### Breaking Changes | ||
| None. This change: | ||
| - Only adds development-mode warnings (not in production) | ||
| - Does not modify runtime behavior | ||
| - Does not change public APIs | ||
| - Is fully backward compatible | ||
|
|
||
| ## Testing | ||
|
|
||
| The new tests can be run with: | ||
| ```bash | ||
| pnpm -C packages/core test delays.test.ts | ||
| ``` | ||
|
|
||
| All existing tests should still pass: | ||
| ```bash | ||
| pnpm test | ||
| ``` | ||
|
|
||
| ## Related Issues | ||
|
|
||
| Addresses the reported issue where: | ||
| - TypeScript doesn't catch unknown delay keys in `after` transitions when using `setup()` | ||
| - Unknown delays cause immediate transitions without warning | ||
| - Developers have difficulty debugging delay configuration mistakes | ||
|
|
||
| ## Benefits | ||
|
|
||
| 1. **Better Developer Experience**: Clear warning messages help catch mistakes early | ||
| 2. **Maintains Performance**: Warning only runs in development mode | ||
| 3. **Backward Compatible**: Existing code continues to work without changes | ||
| 4. **Documented**: Tests and comments clarify the TypeScript limitation |
Sambhram1 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Summary of Changes for PR | ||
|
|
||
| ## Files Modified | ||
|
|
||
| ### 1. `packages/core/src/actions/raise.ts` | ||
| **Change**: Added development-mode warning when an unknown delay key is referenced | ||
|
|
||
| **Code Added**: | ||
| ```typescript | ||
| if (isDevelopment && configDelay === undefined) { | ||
| console.warn( | ||
| `Delay "${delay}" is not configured in \`delays\`. The event will be raised immediately. ` + | ||
| `This is likely a mistake. Available delays: ${Object.keys(delaysMap || {}).join(', ') || 'none'}.` | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. `packages/core/test/setup.types.test.ts` | ||
| **Change**: Updated comments for two test cases to clarify the TypeScript limitation | ||
|
|
||
| **Tests Updated**: | ||
| - "should not accept an after transition that references an unknown delay when delays are configured" | ||
| - "should not accept an after transition that references an unknown delay when delays are not configured" | ||
|
|
||
| **Comment Added**: | ||
| ```typescript | ||
| // @x-ts-expect-error https://github.com/microsoft/TypeScript/issues/55709 | ||
| // TypeScript limitation: mapped types with string unions don't narrow properly. | ||
| // A dev-mode runtime warning will be shown for this. | ||
| ``` | ||
|
|
||
| ### 3. `packages/core/test/delays.test.ts` (NEW FILE) | ||
| **Change**: Created comprehensive runtime tests for delay behavior | ||
|
|
||
| **Tests Added**: | ||
| - ✅ Warns when unknown delay is referenced (with configured delays) | ||
| - ✅ Warns when unknown delay is referenced (without configured delays) | ||
| - ✅ No warning for numeric delays | ||
| - ✅ No warning for properly configured delays | ||
| - ✅ Documents immediate transition behavior for unknown delays | ||
|
|
||
| ## Key Points | ||
|
|
||
| ### Problem Solved | ||
| - Users couldn't catch typos in delay keys at compile time (TypeScript limitation) | ||
| - Unknown delays caused immediate transitions without any warning | ||
| - Difficult to debug delay configuration mistakes | ||
|
|
||
| ### Solution Approach | ||
| - Added development-mode runtime warning (no production overhead) | ||
| - Comprehensive test coverage | ||
| - Documentation of TypeScript limitation | ||
| - Fully backward compatible | ||
|
|
||
| ### TypeScript Limitation | ||
| The issue is due to TypeScript #55709 - mapped types with string unions don't properly narrow in certain contexts. This affects `setup().createMachine()` but NOT `createMachine()` with `types: { delays: ... }`. | ||
|
|
||
| ## How to Use This PR | ||
|
|
||
| ### For PR Description | ||
| Use the content from `/workspaces/xstate/PR_DESCRIPTION.md` | ||
Sambhram1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### For Commit Message | ||
| ``` | ||
| fix(core): add dev-mode warning for unknown delay keys in after transitions | ||
|
|
||
| Adds a development-mode warning when an unknown delay key is referenced | ||
| in `after` transitions. This helps developers catch configuration mistakes | ||
| that TypeScript cannot detect due to a compiler limitation (TS#55709). | ||
|
|
||
| The warning only runs in development mode and does not affect production. | ||
|
|
||
| Fixes: [Issue Number] | ||
| ``` | ||
|
|
||
| ### Testing Instructions | ||
| ```bash | ||
| # Run new delay tests | ||
| pnpm -C packages/core test delays.test.ts | ||
|
|
||
| # Run all tests | ||
| pnpm test | ||
| ``` | ||
Sambhram1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Benefits | ||
| 1. **Better DX**: Clear warnings help catch mistakes early | ||
| 2. **Zero Runtime Cost**: Only runs in development mode | ||
| 3. **Backward Compatible**: No breaking changes | ||
| 4. **Well Documented**: Tests and comments explain the TypeScript limitation | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.