easy-peasy
easy-peasy copied to clipboard
Thunk types on generic model are broken
I noticed calling Thunk on a generic model is broken in TypeScript:
interface Base {
a: string;
}
interface Model<M extends Base> {
data: Generic<M[]>;
add: Action<Model<M>, M>;
doSomething: Thunk<Model<M>, M>;
}
function getModel<M extends Base>(): Model<M> {
return {
data: generic([]),
add: action((state, payload) => {
payload.a + 'foo';
state.data.push(payload);
}),
doSomething: thunk((actions, payload) => {
payload.a;
actions.add(payload);
actions.doSomething(payload); // <-- Argument of type 'M' is not assignable to parameter of type 'void & (M extends undefined ? void : M)'.
}),
};
}
It seems reverting b39dd4e899f634c940fa90b24c8c52b8e59cf557 along with this diff:
diff --git a/index.d.ts b/index.d.ts
index e9b6964..44a6d67 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -227,9 +227,7 @@ type ActionMapper<ActionsModel extends object, K extends keyof ActionsModel> = {
[P in K]: ActionsModel[P] extends Action<any, any>
? ActionCreator<ActionsModel[P]['payload']>
: ActionsModel[P] extends Thunk<any, any, any, any, any>
- ? ActionsModel[P]['payload'] extends void
- ? ThunkCreator<void, ActionsModel[P]['result']>
- : ThunkCreator<ActionsModel[P]['payload'], ActionsModel[P]['result']>
+ ? ThunkCreator<ActionsModel[P]['payload'], ActionsModel[P]['result']>
: ActionsModel[P] extends object
? RecursiveActions<ActionsModel[P]>
: ActionsModel[P];
fixes the problem. I'm not sure why the commit was introduced in the first place, as to me it seems invalid. void is the only reliable way to tell TypeScript that an argument should not be provided. What do you think?
This has been fixed.