seasonedcc / composable-functions

Types and functions to make composition easy and safe
MIT License
649 stars 13 forks source link

Domain functions do not provide compile-time type checks. #131

Closed dan-muller closed 6 months ago

dan-muller commented 6 months ago

For Example:

import { z } from "zod";
import { mdf, pipe } from "domain-functions";

const a = mdf(
  // a schema
  z.object({ aString: z.string() }),
)(
  // a handler
  ({ aString }) => ({ aNumber: Number(aString) }),
);
const b = mdf(
  // b schema
  z.object({ aNumber: z.number() }),
)(
  // b handler
  ({ aNumber }) => ({ aBoolean: aNumber > 0 }),
);
const c = mdf(
  // c schema
  z.object({ aBoolean: z.boolean() }),
)(
  // c handler
  ({ aBoolean }) => ({ gtz: aBoolean }),
);

const d = pipe(a, b, c);

const aResult = await a(
  // the input of `d` should be `{ aString: string }`
  {
    abc: "def", // should not be allowed
    // the compiler should complain about the missing `aString` property
  },
  {}, // should be type `never`
);
const dResult = await d(
  // the input of `d` should be `{ aString: string }`
  {
    abc: "def", // should not be allowed
    // the compiler should complain about the missing `aString` property
  },
  {}, // should be type `never`
);

image

I am not sure if this was intentional but it would make this an even better tool!

diogob commented 6 months ago

Hi @dan-muller , this is intentional and it is documented in our FAQ. It is a consequence of the use of parsers on the input of the domain functions. We are currently creating a new type of function called Composable that will have no parsers, but it will be type-safe.

For more context on these design decisions check issues #80 and #126.

You can also see most of the work on the new Composable type on the PR #121

The composable APIs are already packaged in the latest version of the library, however, they are undocumented and the API is still unstable, so use it at your own risk :wink:

dan-muller commented 6 months ago

Got it! Thanks @diogob

gustavoguichard commented 3 months ago

Hey @dan-muller , just pinging you as we released composable-functions today which is v4 of this library. You should now be able to get type checked arguments of your compositions as expected.

Check out the new README ;)