Description
Bug Report
π Search Terms
inference, type parameters, conditional type, signature resolving
π Version & Regression Information
This is the behavior present in all 4.x versions available on the playground, including 4.5-beta.
β― Playground Link
The originally reported TS playground
π» Code
This is the code for the slimmed-down variant. Note that the createMachine2
is not a part of the repro case but it shows how putting the conditional type on a "different level" makes it work - which I find to be surprising because semantically both variants do the same thing
interface EventObject { type: string; }
interface TypegenDisabled { "@@xstate/typegen": false; }
interface TypegenEnabled { "@@xstate/typegen": true; }
type TypegenConstraint = TypegenEnabled | TypegenDisabled;
interface ActionObject<TEvent extends EventObject> {
type: string;
_TE?: TEvent;
}
declare function assign<TEvent extends EventObject>(
assignment: (ev: TEvent) => void
): ActionObject<TEvent>;
declare function createMachine<
TTypesMeta extends TypegenConstraint = TypegenDisabled
>(
config: {
types?: TTypesMeta;
},
action?: TTypesMeta extends TypegenEnabled
? { action: ActionObject<{ type: "WITH_TYPEGEN" }> }
: { action: ActionObject<{ type: "WITHOUT_TYPEGEN" }> }
): void;
createMachine(
{
types: {} as TypegenEnabled,
},
{
// `action` property is of a correct type - it expects `ActionObject<{ type: "WITH_TYPEGEN" }> }`
action: assign((event) => {
// but the type of this `assign` has been inferred to `ActionObject<{ type: "WITHOUT_TYPEGEN" } | { type: "WITH_TYPEGEN" }>`
// so `event` here is of type `{ type: "WITHOUT_TYPEGEN" } | { type: "WITH_TYPEGEN" }`
((_accept: "WITH_TYPEGEN") => {})(event.type);
}),
}
);
// below we have the code that is not part of the repro case but which is semantically the same as the one above and yet it behaves differently
declare function createMachine2<
TTypesMeta extends TypegenConstraint = TypegenDisabled
>(
config: {
types?: TTypesMeta;
},
action?: {
// it works correctly if we check the `TTypesMeta` further down the road
action: TTypesMeta extends TypegenEnabled
? ActionObject<{ type: "WITH_TYPEGEN" }>
: ActionObject<{ type: "WITHOUT_TYPEGEN" }>;
}
): void;
createMachine2(
{
types: {} as TypegenEnabled,
},
{
action: assign((event) => {
((_accept: "WITH_TYPEGEN") => {})(event.type);
}),
}
);
The code below showcases more accurately what I'm trying to achieve.
Code from the originally reported playground
type Cast<T extends any, TCastType extends any> = T extends TCastType
? T
: TCastType;
type Prop<T, K> = K extends keyof T ? T[K] : never;
type IndexByType<T extends { type: string }> = {
[K in T["type"]]: Extract<T, { type: K }>;
};
interface EventObject {
type: string;
}
interface TypegenDisabled {
"@@xstate/typegen": false;
}
interface TypegenEnabled {
"@@xstate/typegen": true;
}
type TypegenConstraint = TypegenEnabled | TypegenDisabled;
type ResolveTypegenMeta<
TTypesMeta extends TypegenConstraint,
TEvent extends { type: string }
> = TTypesMeta extends TypegenEnabled
? TTypesMeta & {
indexedEvents: IndexByType<TEvent>;
}
: TypegenDisabled;
interface ActionObject<TEvent extends EventObject> {
type: string;
_TE?: TEvent;
}
interface MachineOptions<TEvent extends EventObject> {
actions?: {
[name: string]: ActionObject<TEvent>;
};
}
type TypegenMachineOptions<
TResolvedTypesMeta,
TEventsCausingActions = Prop<TResolvedTypesMeta, "eventsCausingActions">,
TIndexedEvents = Prop<TResolvedTypesMeta, "indexedEvents">
> = {
actions?: {
[K in keyof TEventsCausingActions]?: ActionObject<
Cast<Prop<TIndexedEvents, TEventsCausingActions[K]>, EventObject>
>;
};
};
type MaybeTypegenMachineOptions<
TEvent extends EventObject,
TResolvedTypesMeta = TypegenDisabled
> = TResolvedTypesMeta extends TypegenEnabled
? TypegenMachineOptions<TResolvedTypesMeta>
: MachineOptions<TEvent>;
declare function assign<TEvent extends EventObject>(
assignment: (ev: TEvent) => void
): ActionObject<TEvent>;
declare function createMachine<
TEvent extends { type: string },
TTypesMeta extends TypegenConstraint = TypegenDisabled,
>(
config: {
schema?: {
events?: TEvent;
};
types?: TTypesMeta;
},
options?: MaybeTypegenMachineOptions<TEvent, ResolveTypegenMeta<TTypesMeta, TEvent>>
): void;
interface TypesMeta extends TypegenEnabled {
eventsCausingActions: {
actionName: "BAR";
};
}
createMachine(
{
types: {} as TypesMeta,
schema: {
events: {} as { type: "FOO" } | { type: "BAR"; value: string },
},
},
{
actions: {
// `event` here is not narrowed down to the BAR one
// yet the `actionName`'s type (available when I hover over the property) is correct
actionName: assign((event) => {
((_accept: "BAR") => {})(event.type);
}),
},
}
);
π Actual behavior
event
parameter here is not narrowed down to the WITH_TYPEGEN event, yet the actionName
's type (available when I hover over the property) is "correct" (only WITH_TYPEGEN event there)
π Expected behavior
I would expect this event
parameter to be inferred correctly.
I've been debugging this for a bit and I think this is roughly what happens:
- we have nested
chooseOverload
+inferTypeArguments
calls becauseassign
happens "within"createMachine
typeParameters
forassign
are assigned based on both branches of the conditional type - basically, a union of both is created for this position and since there is an overlap the reduced type gets assigned there.- the "routing" conditional type gets instantiated only after we exit the outer
inferTypeArguments
and it happens withingetSignatureApplicabilityError
but theassign
has been instantiated and cached a step back - withininferTypeArguments
- the inferred type for
assign
is never rechecked - so it stays as if it was supposed to handle both branches of the conditional type
Note that this might be highly incorrect as I'm not too familiar with the codebase