Skip to content
5 changes: 5 additions & 0 deletions .changeset/lucky-geese-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Made `states` type safe
20 changes: 14 additions & 6 deletions packages/core/src/createMachine.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { StateMachine } from './StateMachine.ts';
import {
ResolvedStateMachineTypes,
TODO,
AnyActorRef,
EventObject,
AnyEventObject,
Cast,
EventObject,
InternalMachineImplementations,
MachineConfig,
MachineContext,
MachineTypes,
MetaObject,
NonReducibleUnknown,
ParameterizedObject,
ProvidedActor,
ResolvedStateMachineTypes,
StateValue,
TODO,
ToChildren,
MetaObject
ValidateConfigTargets
} from './types.ts';

type TestValue =
Expand Down Expand Up @@ -88,7 +89,10 @@ export function createMachine<
// it's important to have at least one default type parameter here
// it allows us to benefit from contextual type instantiation as it makes us to pass the hasInferenceCandidatesOrDefault check in the compiler
// we should be able to remove this when we start inferring TConfig, with it we'll always have an inference candidate
_ = any
_ = any,
const TStates extends Record<string, any> | undefined =
| Record<string, any>
| undefined
>(
config: {
types?: MachineTypes<
Expand Down Expand Up @@ -117,7 +121,11 @@ export function createMachine<
TOutput,
TEmitted,
TMeta
>,
> &
{ states?: TStates } &
([TStates] extends [Record<string, any>]
? ValidateConfigTargets<{ states: TStates }>
: {}),
implementations?: InternalMachineImplementations<
ResolvedStateMachineTypes<
TContext,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ToChildren,
ToStateValue,
UnknownActorLogic,
ValidateConfigTargets,
Values
} from './types';

Expand Down Expand Up @@ -249,7 +250,7 @@ export type SetupReturn<
TMeta
>
>(
config: TConfig
config: TConfig & ValidateConfigTargets<TConfig>
) => StateMachine<
TContext,
| TEvent
Expand Down
Loading