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

Piping String to Object fails due to Zip; "Type instantiation is excessively deep" #77

Closed audunolsen closed 1 year ago

audunolsen commented 1 year ago
Screenshot 2023-03-03 at 23 41 05

Attemnpting to make a route-function similar to the example shown in the readme, but simpler. Above code causes ts-error. Type instantiation is excessively deep and possibly infinite. ts(2589). Toggling Tuples.Zip seems to trigger the error. In above example zip is utilised to transform ["user", "friend"] to [["user"], ["friend"]] and then mapping the latter to [["user", string], ["friend", string]] and then ultimately to the final object. Thoughts?

TS-playground repro.

audunolsen commented 1 year ago
Screenshot 2023-03-04 at 00 09 47

I guess I can just do this instead of course, which works fine.

I'm new to this lib and this approach to typescript, so I may have misunderstood the purpose of zip. I thought you could wrap any value in a tuple with it? So I'm not sure if this is an actual issue or just me not understanding it properly.

gvergnaud commented 1 year ago

Weird, this look like some sort of bug. I'll take a look.

Here is an alternative implementation using Objects.Create, which takes a data structure and evaluates any function inside of it with the current arguments:

type RouteParams<T extends string> = Pipe<
  T,
  [
    Strings.Split<"/">,
    Tuples.Filter<Strings.StartsWith<":">>,
    Tuples.Map<Objects.Create<[Strings.Trim<":">, string]>>,
    Tuples.ToUnion,
    Objects.FromEntries
  ]
>;

type x = RouteParams<"/users/:user/friends/:friend">
gvergnaud commented 1 year ago

I think I fixed the issue: Playground