Skip to content

refactor: replace overloaded functions with mapped types #72

Description

@barona-mika-vilpas

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:

  1. 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]>
  1. 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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions