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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal for the base function

ecyrbe opened this issue · comments

commented

We maybe can setup Fn to be generic of input types. like so :

interface Fn<T=unknown> {
  input: T;
  output: unknown;
}

This allows to implement specialised algorithms (for arrays, strings, numbers) without having to type cast, exemple for pipe:

interface PipeFn extends Fn<{
    acc: unknown;
    item: Fn;
  }> {
  output: Call<this["input"]["item"], this["input"]["acc"]>
}

Making these actually easier to read

commented

After some testing, this proves to not work if T is a complex object like

type ReduceParam = {
    acc: unknown;
    item: Fn;
}

I'm going for this instead:

export interface Fn {
    args: unknown[];
    output: unknown;
  }

This makes dealing with function arguments super intuitive IMO:

export interface Head extends HOT.Fn {
   output: HeadImpl<this["args"][0]>;
 }

// or the more complex
interface MapFn<fn extends HOT.Fn> extends HOT.Fn {
   output: this["args"] extends [infer acc extends any[], infer item]
     ? [...acc, HOT.Call<fn, item>]
     : never;
 }