/hotscript

Type-level madness

Primary LanguageTypeScript

Higher-Order TypeScript (HOTScript)

A lodash-like library for types, with support for type-level lambda functions.

🚧 work in progress 🚧

// prettier-ignore
type res1 = Pipe<
  //  ^? 95
  [1, 2, 3, 4, 3, 4],
  [
    Tuples.Map<Numbers.Add<3>>,
    Tuples.Join<".">,
    Strings.Split<".">,
    Tuples.Map<Strings.ToNumber>,
    Tuples.Map<Numbers.Add<10>>,
    Tuples.Sum
  ]
>;

// This is a type-level "lambda"!
interface Duplicate extends Fn {
  return: [this["arg0"], this["arg0"]];
}

type result1 = Call<Tuples.Map<Duplicate>, [1, 2, 3, 4]>;
//     ^? [[1, 1], [2, 2], [3, 3], [4, 4]]

type result2 = Call<Tuples.FlatMap<Duplicate>, [1, 2, 3, 4]>;
//     ^? [1, 1, 2, 2, 3, 3, 4, 4]

// Let's compose some functions to transform an object type:
type ToAPIPayload<T> = Pipe<
  T,
  [
    Objects.OmitBy<Booleans.Equals<symbol>>,
    Objects.Assign<{ metadata: { newUser: true } }>,
    Objects.SnakeCaseDeep,
    Objects.Assign<{ id: string }>
  ]
>;
type T = ToAPIPayload<{
  id: symbol;
  firstName: string;
  lastName: string;
}>;
// Returns:
type T = {
  id: string;
  metadata: { new_user: true };
  first_name: string;
  last_name: string;
};

TODO

  • Function
    • Pipe
    • PipeRight
    • Call
    • Apply
    • PartialApply
    • Compose
    • ComposeLeft
  • Tuples
    • Create
    • Partition
    • IsEmpty
    • Zip
    • ZipWith
    • Sort
    • Head
    • At
    • Tail
    • Last
    • FlatMap
    • Find
    • Sum
    • Drop n
    • Take n
    • TakeWhile
    • Join separator
    • Map
    • Filter
    • Reduce
    • ReduceRight
    • Every
    • Some
  • Object
    • Get
    • FromEntries
    • Entries
    • MapValues
    • MapKeys
    • GroupBy
    • Assign
    • Pick
    • PickBy
    • Omit
    • OmitBy
    • CamelCase
    • CamelCaseDeep
    • SnakeCase
    • SnakeCaseDeep
    • KebabCase
    • KebabCaseDeep
  • Union
    • Map
    • Extract
    • ExtractBy
    • Exclude
    • ExcludeBy
  • String
    • Length
    • TrimLeft
    • TrimRight
    • Trim
    • Join
    • Replace
    • Slice
    • Split
    • Repeat
    • StartsWith
    • EndsWith
    • ToTuple
    • ToNumber
    • ToString
    • Prepend
    • Append
    • Uppercase
    • Lowercase
    • Capitalize
    • Uncapitalize
    • SnakeCase
    • CamelCase
    • KebabCase
    • Compare
    • Equal
    • NotEqual
    • LessThan
    • LessThanOrEqual
    • GreaterThan
    • GreaterThanOrEqual
  • Number
    • Add
    • Multiply
    • Subtract
    • Negate
    • Power
    • Div
    • Mod
    • Abs
    • Compare
    • GreaterThan
    • GreaterThanOrEqual
    • LessThan
    • LessThanOrEqual
  • Boolean
    • And
    • Or
    • Not
    • Extends
    • Equals
    • DoesNotExtend