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.38k stars 57 forks source link

feat(match): allow to match strictly and customize predicate #106

Closed ecyrbe closed 6 months ago

ecyrbe commented 11 months ago

This allows to match exactly and also customize the predicate to match or pass a checker.
So with this, you can match types only. So not just check if something extends a string, but check that something is actually a type string:


interface ExtendsTypeOf extends Fn {
  return: this["arg0"] extends this["arg1"] ? true: false;
}

interface IsArray extends Fn {
  return: this["arg0"] extends unknown[] ? true: false;
}

type TestTypes<T> = Match<T,[
   Match.WithEqual<string, "string">,
   Match.WithEqual<number, "number">,
   Match.WithEqual<boolean, "boolean">,
   Match.WithEqual<null, "null">,
   Match.WithPredicate<ExtendsTypeOf, Record<string,unknown> , "object">, // same as `Match.With<Record<string, unknown>, "object">`
   Match.With<IsArray , "array">, // same as `Match.With<unknown[], "array">`
   Match.With<any, "not a json type">
  ]>;
ecyrbe commented 11 months ago

I'm not found of my naming here. suggestions are welcomed