refactor: replace overloaded functions with mapped types
Opened this issue · 1 comments
barona-mika-vilpas commented
Currently parjs has some functions that are overloaded, such as this:
export function pipe<T, T1, T2, T3, T4, T5, T6>(
source: ImplicitParjser<T>,
cmb1: ParjsCombinator<T, T1>,
cmb2: ParjsCombinator<T1, T2>,
cmb3: ParjsCombinator<T2, T3>,
cmb4: ParjsCombinator<T3, T4>,
cmb5: ParjsCombinator<T4, T5>,
cmb6: ParjsCombinator<T5, T6>
): Parjser<T6>;
The goals:
- see if it's possible to get rid of overloading and replace it with something else, such as a map type or a tuple type
type MapParjsers<Vs extends any[] = {
[K in keyof Vs]: ImplicitParjser<Vs[K]>
}
export function then<Head, Tail extends any[]>(first: ImplicitParjser<Head>, ...following: MapParjsers<Tail>): ParjsCombinator<[Head, ...Tail]>
- make sure to preserve the type inference! Calling these must be type safe, i.e. the return type must be perfectly inferred (right now it's good)
This came up in #64 (comment)
barona-mika-vilpas commented
idea:
There are some interesting suggestions in the comment section here. They might be helpful:
https://dev.to/futuresight/how-2-typescript-get-the-last-item-type-from-a-tuple-of-types-3fh3