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

Working with function types #100

Open jasonkuhrt opened 1 year ago

jasonkuhrt commented 1 year ago

Examples:

Would this be in scope for this project?

gvergnaud commented 1 year ago

Maybe a few functions like Functions.MapArguments<Mapper> and Functions.MapReturnType<Mapper> could be added to support these use cases?

type DoStuff<Fn> = Pipe<Fn, [
  // Remove a union member from the return type
  Functions.MapReturnType<Union.Exclude<"a">>,
  // add a union member to the return type
  Functions.MapReturnType<Union.Append<"b">>,
  // remove a parameter
  Functions.MapArguments<Tuples.Filter<Boolean.Extends<number>>>,
  // add a parameter
  Functions.MapArguments<Tuples.Append<"1" | "2" | "3">>,
  // map over the parameters
  Functions.MapArguments<Tuples.Map<Strings.ToNumber>>,
]>

What do you think?

markerikson commented 1 year ago

Potentially useful, yeah.

Going back to the Reselect MergeParameters example I'd linked at https://github.com/reduxjs/reselect/blob/v4.1.5/src/types.ts#L100-L148 (and the updated version from Anders Hejlsberg at https://github.com/reduxjs/reselect/blob/b2a85eeac3312bb62ae3c26bb44d9aabd8641552/src/versionedTypes/ts47-mergeParameters.ts )... what might those look like with this style of typing?

gvergnaud commented 1 year ago

Added Functions.MapParameters and Functions.MapReturnType to the latest release! let me know what you think