Target is to infer the type of one of the properties the return value of a react hook. This should happens by extending the base type which is inferred from the parameter of the react hook. The extension is done by providing optional map to the react hook named userDefinedActions which transform the base type.

The inheritance should work via 3 packages:

Customer application code → @brease/react package → @brease/core package

Working single file/project baseline

I sort of managed to get it working without the zustand store and without the react hook in the same codebase.

The inferred type for the below example for the result variable should look like this on vs code hover as well and not just be available at runtime:

CleanShot 2023-07-04 at 21.05.03@2x.png

Dynamic? Type inheritance

Even this is not 100% right… I’d be happiest if the resulting type would be:

const b: User & {
    some: string;
} & {
    code: string;
} & {
    taxes: string;
}

The monorepo / multi-package problem

Here is the monorepo with the example nextjs code: https://github.com/dotindustries/brease/tree/feature/bre-14-react-hooks

The react hook: https://github.com/dotindustries/brease/blob/8d20c64bb4ed5c36829d4f08d21ac50f1b4228e6/packages/react/src/hooks.ts#L52

The vanilla zustand store for the framework agnostic core package: https://github.com/dotindustries/brease/blob/8d20c64bb4ed5c36829d4f08d21ac50f1b4228e6/packages/core/src/store.ts#L44

What I currently have is seemingly working since 2 minutes:

Code is linked above for the branch with the latest code.

https://github.com/dotindustries/brease/blob/8d20c64bb4ed5c36829d4f08d21ac50f1b4228e6/examples/next/src/pages/index.tsx#L67

const { result: order } = useRules("checkout", user, {
    objectID: user.user_id,
    userDefinedActions: {
      async $set(o) {
        const result: CheckoutValues = {
          shipping: "$ 8.00",
        };
        return { ...o, ...result };
      },
    },
  });