gvergnaud / hotscript

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.
3.51k stars 59 forks source link

feat(objects): generalized args #54

Closed gvergnaud closed 1 year ago

gvergnaud commented 1 year ago

Motivation

The idea is to turn the args, arg0, arg{n} keywords into functions. This has 2 benefits:

  1. This make them usable in more context, and make them composable with other functions:
type res1 = Eval<
   Tuples.Reduce<args, [], [1, 2, 3]> 
>
// => [[[[], 1], 2], 3]

type res2 = Eval<
   Tuples.ZipWith<args, ['a', 'b', 'c'], [1, 2, 3]> 
>
// => [['a', 1], ['b', 2], ['c', 3]]
  1. It means we can generalize Objects.Create to work with any nested function:
type res1 = Call<
        //   ^?
        Objects.Create<{
          addition: Numbers.Add<10, _>;
          division: Numbers.Div<_, 2>;
          nested: [Numbers.GreaterThan<0>];
          recursion: Functions.ComposeLeft<
            [
              Objects.Create<{
                label: Strings.Prepend<"number: ">;
                content: Strings.Append<" is the number we got!">;
              }>,
              Objects.Create<{
                post: arg0;
              }>
            ]
          >;
        }>,
        10
      >;
      type test2 = Expect<
        Equal<
          res1,
          {
            addition: 20;
            division: 5;
            nested: [true];
            recursion: {
              post: {
                label: "number: 10";
                content: "10 is the number we got!";
              };
            };
          }
        >
      >;