easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

Thunk types on generic model are broken

Open methyl opened this issue 5 years ago • 1 comments

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)'.
     }),
  };
}

methyl avatar Nov 09 '20 16:11 methyl

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?

methyl avatar Nov 09 '20 16:11 methyl

This has been fixed.

ctrlplusb avatar Sep 16 '22 06:09 ctrlplusb