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

Type Aliases Not Working With 'Thunk' Type

Open LuisOsta opened this issue 3 years ago • 2 comments

Easy Peasy Version

"easy-peasy": "^5.0.3",

Context React SPA Application using CRA. Currently running React 16.9.

So in order to keep my types DRY I tried to define the type for one of my thunks as:

export type ChangeUserThunk = Thunk<
  EmployeeModel,
  { newRole: USER_ROLE; userId: string },
  Injections,
  IStoreModel
>;

Then use them in my model definition like this:

export interface EmployeeModel {
  changeUserRole: ChangeUserThunk;
}

And with my thunk like this:

export const changeUserRole: ChangeUserThunk = thunk(
  async (actions, payload, { getStoreState, injections }) => {
    const { newRole, userId } = payload;
    const { userService } = injections;
    const token = getStoreState().user.token;

    try {
      const updatedEmployee = await userService.updateRole(
        userId,
        newRole,
        token
      );
      actions.updateEmployeeById(updatedEmployee);
    } catch (e) {
      console.error(e);
    }
  }
);

The problem is when taking this approach the actions, payload, getStoreState, etc are all defined with the default types. So I get these error:

Property 'user' does not exist on type 'StateMapper<_Pick<{}, never>>'.ts(2339)
Property 'updateEmployeeById' does not exist on type 'Actions<{}>'.ts(2339)

Notably I also get an error in with the 'ts-toolbet' package. Its as follows:

/app/node_modules/ts-toolbelt/out/Any/At.d.ts
client_1  | TypeScript error in /app/node_modules/ts-toolbelt/out/Any/At.d.ts(24,121):
client_1  | Type expected.  TS1110
client_1  | 
client_1  |     22 |  * ```
client_1  |     23 |  */
client_1  |   > 24 | export declare type At<A extends any, K extends Key> = A extends List ? number extends A['length'] ? K extends number | `${number}` ? A[never] | undefined : undefined : K extends keyof A ? A[K] : undefined : unknown extends A ? unknown : K extends keyof A ? A[K] : undefined;
client_1  |        |                                                                                                                         ^
client_1  |     25 |

Important Note When switching from the alias method to the spelled out method. It still does not resolve the toolbelt issues.

LuisOsta avatar Mar 05 '21 02:03 LuisOsta

By upgrading I think I was able to solve the toolbelt problem. But the alias thing persists

LuisOsta avatar Mar 05 '21 03:03 LuisOsta

i have the same issue, it happend when i updated from ~4.1.5 to 4.2.4

macrozone avatar Apr 09 '21 09:04 macrozone

Is this still an issue with the latest version?

jmyrland avatar Sep 16 '22 18:09 jmyrland

@jmyrland I've just updated to easy-peasy 5.1.0, and typescript 4.8.4 and I have this same issue

arielhs avatar Nov 09 '22 02:11 arielhs

@jmyrland I've just updated to easy-peasy 5.1.0, and typescript 4.8.4 and I have this same issue

~Hi @arielhs ! Thanks for reporting. Would you mind creating a new issue? Preferably with a code sandbox link reproducing the issue?~

Edit: just saw your PR now - will take a look at it

jmyrland avatar Nov 09 '22 06:11 jmyrland