statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.23k stars 1.26k forks source link

Bug: Emit cannot infer type #5038

Closed oskar-martensson closed 3 months ago

oskar-martensson commented 3 months ago

XState version

XState version 5

Description

Type of emit cannot be inferred when when defining an action.

Example

setup({
  types: {
    emitted: {} as { type: 'apple', value: string } | { type: 'banana', value: number }
  },
  actions: {
    appleAction: emit({ type: 'apple', value: 'fruit' }),
    bananaAction: emit({ type: 'banana', value: 2 })
  },
});

Expected result

Type should be inferred.

Actual result

Type '{ type: string; value: string; }' is not assignable to type '{ type: "apple"; value: string; } | { type: "banana"; value: number; }'.

Reproduction

https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240331#code/JYWwDg9gTgLgBAbzgZwKYwK5gDR1SYeAXzgDMoIQ4AiAD2RgEMZVqBuOAek7hgE8wqZAC44AVgB0ATgkBGAFDy0mMAAoE8uLwFDRGrVvyEWAEz0lGyRNsGiA5IzBgANqju4Abo2cZUohlDAAHYA5nAkAD7W-LZwdgBGjEFJjO5wXj5+cEEYIPGoUOGa4djFjADGMMAQQSKIxVqOLqgAgpXVQaJGMOo2WQ5OrmkZvvbkGIR24QCUpQZwicnJbVU1XQQ9SDH9iynD3qNwAEwzxUSlRNNs8kA

Additional context

No response

davidkpiano commented 3 months ago

@Andarist Is this the same issue as #4545 ?