-
Notifications
You must be signed in to change notification settings - Fork 33
Open
Labels
Type: MaintenanceAny dependency, housekeeping, and clean up Issue or PRAny dependency, housekeeping, and clean up Issue or PRtypescriptRelevant to TypeScript users onlyRelevant to TypeScript users only
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 :).
Metadata
Metadata
Assignees
Labels
Type: MaintenanceAny dependency, housekeeping, and clean up Issue or PRAny dependency, housekeeping, and clean up Issue or PRtypescriptRelevant to TypeScript users onlyRelevant to TypeScript users only