Skip to content

PaginateInterface & ComposePaginateInterface - code duplication issue #286

Open
@ptomulik

Description

I've seen this comment and I came across the following idea.

interface Overloads {
    0: (t1: number) => number;
    1: (t1: number, t2: number) => number;
}

interface Generate<Prepend extends []|[string]> {
    (...args: [...Prepend, ...Parameters<Overloads[0]>]): ReturnType<Overloads[0]>;
    (...args: [...Prepend, ...Parameters<Overloads[1]>]): ReturnType<Overloads[1]>;
}

type Normal = Generate<[]>; // Generate Overloads
type Prepended = Generate<[string]>; // Generate Overloads with leading argument of type string

const normal: Normal = (t1: number, t2?: number) => {
    return t1 + (t2 === undefined ? 0 : t2);
}

const prepended: Prepended = (s: string, t1: number, t2?: number) => {
    console.log(s);
    return t1 + (t2 === undefined ? 0 : t2);
}

console.log(normal(1));
console.log(normal(1, 2));
console.log(prepended("one", 1));
console.log(prepended("two plus three", 2, 3));

Technically this approach could do the job, but there are three problems - argument names get lost, doc-strings get lost and the final interfaces/types become completely unreadable :).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type: MaintenanceAny dependency, housekeeping, and clean up Issue or PRtypescriptRelevant to TypeScript users only

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions