-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
Milestone
Description
π Search Terms
silent never leak reverse mapped types nested calls return type inference
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» Code
type Values<T> = T[keyof T];
type MachineContext = Record<string, any>;
interface ParameterizedObject {
type: string;
params?: unknown;
}
type ActionFunction<
TContext extends MachineContext,
TParams extends ParameterizedObject["params"] | undefined,
TAction extends ParameterizedObject,
> = {
(ctx: TContext, params: TParams): void;
_out_TAction?: TAction;
};
type ToParameterizedObject<
TParameterizedMap extends Record<
string,
ParameterizedObject["params"] | undefined
>,
> = Values<{
[K in keyof TParameterizedMap & string]: {
type: K;
params: TParameterizedMap[K];
};
}>;
type CollectActions<
TContext extends MachineContext,
TParams extends ParameterizedObject["params"] | undefined,
> = (
{
context,
enqueue,
}: {
context: TContext;
enqueue: (action: () => void) => void;
},
params: TParams,
) => void;
declare function enqueueActions<
TContext extends MachineContext,
TParams extends ParameterizedObject["params"] | undefined,
TAction extends ParameterizedObject = ParameterizedObject,
>(
collect: CollectActions<TContext, TParams>,
): ActionFunction<TContext, TParams, TAction>;
declare function setup<
TContext extends MachineContext,
TActions extends Record<
string,
ParameterizedObject["params"] | undefined
> = {},
>({
types,
actions,
}: {
types?: { context?: TContext };
actions?: {
[K in keyof TActions]: ActionFunction<
TContext,
TActions[K],
ToParameterizedObject<TActions>
>;
};
}): void;
setup({
actions: {
doStuff: enqueueActions((_, params: number) => {}),
},
});
setup({
actions: {
doStuff: enqueueActions((_, params: number) => {}),
doOtherStuff: (_, params: string) => {},
},
});
setup({
actions: {
doStuff: enqueueActions((_, params: number) => {}),
doOtherStuff: (_: any, params: string) => {},
},
});π Actual behavior
First 2 calls mention such a relationship check failure in the error message:
Type 'ActionFunction<MachineContext, number, { type: string; params: never; }>' is not assignable to type 'ActionFunction<MachineContext, number, { type: "doStuff"; params: number; }>'.
Notice never there. This is quite weird an unexpected as there is no never in sight here. It turns out this is a silentNeverType that leaked through.
π Expected behavior
At the very least, I would expect it to error with consistent error messages mentioning unknown instead of never. Even better, if it could behave closer to a very similar version of this code: TS playground. In there the third error still errors but the first two infers nicely.
Additional information about the issue
No response