Skip to content

Commit

Permalink
[core] Remove typegen types (#4949)
Browse files Browse the repository at this point in the history
* One down

* Default to typegen disabled

* Remove stuff

* More progress

* Some surgery

* Actors

* This is fun

* This is fun

* More deletion

* Remove ResolveTypegenMeta type param

* Renaming

* Cleanup

* Remove typgenTypes files + mentions

* Simplification

* Rename TResolvedTypesMeta -> TTypes

* More simplification

* add helper

* use the helper

* use the helper in one extra place

* bring back relation between MachineTypes and SetupTypes

* break TS 5.0

* Rename `ResolveTypegenMeta` -> `ResolvedStateMachineTypes`

* Remove `__TResolvedTypesMeta`

* Changeset

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
  • Loading branch information
davidkpiano and Andarist authored Jul 10, 2024
1 parent 8b4167f commit 8aa4c2b
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 2,055 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-crabs-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': minor
---

The TypeGen-related types have been removed from XState, simplifying the internal types without affecting normal XState usage.
1 change: 0 additions & 1 deletion packages/core/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { $$ACTOR_TYPE } from './createActor.ts';
import type { StateNode } from './StateNode.ts';
import type { StateMachine } from './StateMachine.ts';
import { getStateValue } from './stateUtils.ts';
import { TypegenDisabled } from './typegenTypes.ts';
import type {
ProvidedActor,
AnyMachineSnapshot,
Expand Down
37 changes: 14 additions & 23 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
transitionNode
} from './stateUtils.ts';
import { AnyActorSystem } from './system.ts';
import { ResolveTypegenMeta, TypegenDisabled } from './typegenTypes.ts';
import type {
ActorLogic,
ActorScope,
Expand All @@ -46,7 +45,8 @@ import type {
SnapshotFrom,
StateMachineDefinition,
StateValue,
TransitionDefinition
TransitionDefinition,
ResolvedStateMachineTypes
} from './types.ts';
import { resolveReferencedActor, toStatePath } from './utils.ts';

Expand All @@ -66,17 +66,7 @@ export class StateMachine<
TInput,
TOutput,
TEmitted extends EventObject = EventObject, // TODO: remove default
TMeta extends MetaObject = MetaObject,
TResolvedTypesMeta = ResolveTypegenMeta<
TypegenDisabled,
DoNotInfer<TEvent>,
TActor,
TAction,
TGuard,
TDelay,
TTag,
TEmitted
>
TMeta extends MetaObject = MetaObject
> implements
ActorLogic<
MachineSnapshot<
Expand Down Expand Up @@ -187,9 +177,16 @@ export class StateMachine<
*/
public provide(
implementations: InternalMachineImplementations<
TContext,
TResolvedTypesMeta,
true
ResolvedStateMachineTypes<
TContext,
DoNotInfer<TEvent>,
TActor,
TAction,
TGuard,
TDelay,
TTag,
TEmitted
>
>
): StateMachine<
TContext,
Expand All @@ -204,8 +201,7 @@ export class StateMachine<
TInput,
TOutput,
TEmitted,
TMeta, // TMeta
TResolvedTypesMeta
TMeta
> {
const { actions, guards, actors, delays } = this.implementations;

Expand Down Expand Up @@ -619,9 +615,4 @@ export class StateMachine<

return restoredSnapshot;
}

/**
* @deprecated an internal property that was acting as a "phantom" type, it's not used by anything right now but it's kept around for compatibility reasons
**/
__TResolvedTypesMeta!: TResolvedTypesMeta;
}
4 changes: 2 additions & 2 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ export class StateNode<
any, // action
any, // guard
any, // delay
any, // state value
any, // tag
any, // input
any, // output
any, // emitted
any, // TMeta
any // typegen
any // meta
>;
/**
* The meta data associated with this state node, which will be returned in State instances.
Expand Down
54 changes: 10 additions & 44 deletions packages/core/src/createMachine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { StateMachine } from './StateMachine.ts';
import {
ResolveTypegenMeta,
TypegenConstraint,
TypegenDisabled
} from './typegenTypes.ts';
import { ResolvedStateMachineTypes } from './types.ts';
import {
AnyActorRef,
EventObject,
Expand All @@ -16,7 +12,6 @@ import {
MachineTypes,
NonReducibleUnknown,
ParameterizedObject,
Prop,
ProvidedActor,
StateValue,
ToChildren,
Expand Down Expand Up @@ -122,7 +117,7 @@ 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
TTypesMeta extends TypegenConstraint = TypegenDisabled
_ = any
>(
config: {
types?: MachineTypes<
Expand All @@ -136,8 +131,7 @@ export function createMachine<
TInput,
TOutput,
TEmitted,
TMeta,
TTypesMeta
TMeta
>;
schemas?: unknown;
} & MachineConfig<
Expand All @@ -151,13 +145,11 @@ export function createMachine<
TInput,
TOutput,
TEmitted,
TMeta,
TTypesMeta
TMeta
>,
implementations?: InternalMachineImplementations<
TContext,
ResolveTypegenMeta<
TTypesMeta,
ResolvedStateMachineTypes<
TContext,
TEvent,
TActor,
TAction,
Expand All @@ -175,37 +167,12 @@ export function createMachine<
TAction,
TGuard,
TDelay,
'matchesStates' extends keyof TTypesMeta
? ToStateValue<Cast<TTypesMeta['matchesStates'], TestValue>>
: StateValue,
Prop<
ResolveTypegenMeta<
TTypesMeta,
TEvent,
TActor,
TAction,
TGuard,
TDelay,
TTag,
TEmitted
>['resolved'],
'tags'
> &
string,
StateValue,
TTag & string,
TInput,
TOutput,
TEmitted,
TMeta, // TMeta
ResolveTypegenMeta<
TTypesMeta,
TEvent,
TActor,
TAction,
TGuard,
TDelay,
TTag,
TEmitted
>
TMeta // TMeta
> {
return new StateMachine<
any,
Expand All @@ -220,7 +187,6 @@ export function createMachine<
any,
any,
any, // TEmitted
any, // TMeta
any
any // TMeta
>(config as any, implementations as any);
}
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export { type Spawner } from './spawn.ts';
export { isMachineSnapshot, type MachineSnapshot } from './State.ts';
export { StateMachine } from './StateMachine.ts';
export { getStateNodes } from './stateUtils.ts';
export * from './typegenTypes.ts';
export * from './types.ts';
export { waitFor } from './waitFor.ts';
import { createMachine } from './createMachine.ts';
Expand Down
26 changes: 3 additions & 23 deletions packages/core/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StateMachine } from './StateMachine';
import { createMachine } from './createMachine';
import { GuardPredicate } from './guards';
import { ResolveTypegenMeta, TypegenDisabled } from './typegenTypes';

import {
ActionFunction,
AnyActorRef,
Expand Down Expand Up @@ -196,17 +196,7 @@ export function setup<
TInput,
TOutput,
TEmitted,
TMeta,
ResolveTypegenMeta<
TypegenDisabled,
TEvent,
ToProvidedActor<TChildrenMap, TActors>,
ToParameterizedObject<TActions>,
ToParameterizedObject<TGuards>,
TDelay,
TTag,
TEmitted
>
TMeta
>
>(
config: TConfig
Expand All @@ -226,17 +216,7 @@ export function setup<
TInput,
TOutput,
TEmitted,
TMeta,
ResolveTypegenMeta<
TypegenDisabled,
TEvent,
ToProvidedActor<TChildrenMap, TActors>,
ToParameterizedObject<TActions>,
ToParameterizedObject<TGuards>,
TDelay,
TTag,
TEmitted
>
TMeta
>;
} {
return {
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AnyEventObject,
AnyMachineSnapshot,
ConditionalRequired,
GetConcreteByKey,
InputFrom,
IsLiteralString,
IsNotNever,
Expand Down Expand Up @@ -35,22 +36,14 @@ type SpawnOptions<
>
: never;

// it's likely-ish that `(TActor & { src: TSrc })['logic']` would be faster
// but it's only possible to do it since https://github.com/microsoft/TypeScript/pull/53098 (TS 5.1)
// and we strive to support TS 5.0 whenever possible
type GetConcreteLogic<
TActor extends ProvidedActor,
TSrc extends TActor['src']
> = Extract<TActor, { src: TSrc }>['logic'];

export type Spawner<TActor extends ProvidedActor> = IsLiteralString<
TActor['src']
> extends true
? {
<TSrc extends TActor['src']>(
logic: TSrc,
...[options]: SpawnOptions<TActor, TSrc>
): ActorRefFrom<GetConcreteLogic<TActor, TSrc>>;
): ActorRefFrom<GetConcreteByKey<TActor, 'src', TSrc>['logic']>;
<TLogic extends AnyActorLogic>(
src: TLogic,
options?: {
Expand Down
Loading

0 comments on commit 8aa4c2b

Please sign in to comment.