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

Working with function types

jasonkuhrt opened this issue · comments

Examples:

  • Remove a union member from the return type
  • add a union member to the return type
  • remove a paramter
  • add a paramter
  • map over the paramters
  • ...

Would this be in scope for this project?

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?

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?

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