Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withIn/withOut methods for constraining one side of a morph #1119

Open
ssalbdivad opened this issue Sep 5, 2024 · 0 comments
Open

withIn/withOut methods for constraining one side of a morph #1119

ssalbdivad opened this issue Sep 5, 2024 · 0 comments

Comments

@ssalbdivad
Copy link
Member

This would allow the inputs or outputs of morphs to be directly constrained via chaining as opposed to having to intersect (for input) or pipe (for output) from scratch.

I did some initial work defining the API, but there are nuances around how branches are mapped when the input is a union that would be tough to capture so will require a bit more thought:

// in ark/type/methods/morph.ts

interface Type<out t = unknown, $ = {}> extends BaseTyp<t, $> {
  // other methods

  withIn<mappedIn extends this["inferBrandableIn"]>(
    mapIn: (In: this["in"]) => BaseType & { inferBrandableIn: mappedIn }
  ): Type<
    this["inferIntrospectableOut"] extends this["inferBrandableOut"]
      ? (In: mappedIn) => To<this["inferBrandableOut"]>
      : (In: mappedIn) => Out<this["inferBrandableOut"]>,
    $
  >;

  withOut<mappedOut extends this["inferBrandableOut"]>(
    mapOut: (In: this["out"]) => BaseType & { inferBrandableOut: mappedOut }
  ): Type<(In: this["inferBrandableIn"]) => To<mappedOut>, $>;
}

I also wrote an initial test:

it("withIn", () => {
  const t = type("string")
    .pipe((s) => s.length)
    .withIn((t) => t.atLeastLength(1));

  const expected = type("string >= 1").pipe((s) => s.length);

  attest(t.expression).equals(expected.expression);
  attest<typeof expected.t>(t.t);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant