Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why specialize to 10 arguments? #2

Open
reverofevil opened this issue Dec 25, 2018 · 5 comments
Open

Why specialize to 10 arguments? #2

reverofevil opened this issue Dec 25, 2018 · 5 comments

Comments

@reverofevil
Copy link

reverofevil commented Dec 25, 2018

I think something in lines of

type Head<T extends any[]> = T extends [infer X, ...any[]] ? X : never;

type Tail<T extends any[]> = ((...x: T) => void) extends ((x: any, ...xs: infer XS) => void) ? XS : never

type Cons<X, XS extends any[]> = ((h: X, ...args: XS) => void) extends ((...args: infer R) => void) ? R : [];

type Reverse<L extends any[], X extends any[] = []> = (
	L extends [] ? { [indirect]: X } : { [indirect]: Reverse<Tail<L>, Cons<Head<L>, X>> }
)[typeof indirect];

// for some reason `indirect` hack doesn't work here
type MapTuple<X extends any[], S extends any[], R extends any[] = []> = {
	0: Reverse<R>,
	1: MapTuple<Tail<X>, S, Cons<$<Head<X>, S>, R>>
}[X extends [] ? 0 : 1]

type $<T, S extends any[]> = (
	T extends _<infer N> ? { [indirect]: S[N] } :
	T extends undefined | null | boolean | string | number ? { [indirect]: T } :
	T extends any[] ? { [indirect]: MapTuple<T, S> } :
	T extends (...x: infer I) => infer O ? { [indirect]: (...x: $<I, S>) => $<O, S> } :
	T extends object ? { [indirect]: { [K in keyof T]: $<T[K], S> } } :
	{ [indirect]: T }
)[typeof indirect];

should be possible to use.

@reverofevil
Copy link
Author

Partially taken from here.

@reverofevil
Copy link
Author

It quickly turned out that _ can be passed only to something like data constructors, i.e. generic types that have no preference on their parameters. As Head<_> didn't work, I quickly abandoned the idea.

@pelotom
Copy link
Owner

pelotom commented Dec 25, 2018

This library was written before TS 3.0 and hasn’t been updated to take advantage of rest parameter tuple types. A PR would be welcome, although it sounds like you’re saying there’s an unanticipated problem?

@reverofevil
Copy link
Author

It's an unanticipated problem in the library in its current state. It's impossible to create a type-lambda for a type that explicitly asks its parameter to extend something, like Head<T extends any[]>. It's thus not very useful for me.

(It took me an hour to realize that I know you as author of effectful library. A rare coincidence.)

@pelotom
Copy link
Owner

pelotom commented Dec 25, 2018

Yep, that’s me 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants