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

PaginateInterface & ComposePaginateInterface - code duplication issue #286

Open
ptomulik opened this issue Mar 11, 2021 · 0 comments
Open
Labels
Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR typescript Relevant to TypeScript users only
Projects

Comments

@ptomulik
Copy link
Contributor

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 :).

@ghost ghost added this to Inbox in JS Mar 11, 2021
@gr2m gr2m added Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR typescript Relevant to TypeScript users only labels Mar 11, 2021
@ghost ghost moved this from Inbox to Maintenance in JS Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR typescript Relevant to TypeScript users only
Projects
No open projects
JS
  
Maintenance
Development

No branches or pull requests

2 participants