I am currently in the process of adding TypeScript typings to a third-party JavaScript library. The majority of the class methods require another instance of the same class as input and return a new instance. However, they also accept overloaded constructor arguments in place of an actual instance. I am looking for a way to avoid repeating the same overloaded signature for each method within the class in my .d.ts
file.
For instance, I could manually list out each method like so...
class Foo {
constructor(x: number);
constructor(x: number, y: number);
constructor({ x: number, y: number });
Fizz(foo: Foo): Foo;
Fizz(x: number): Foo;
Fizz(x: number, y: number): Foo;
Fizz({ x: number, y: number }): Foo;
Buzz(foo: Foo): Foo;
Buzz(x: number): Foo;
Buzz(x: number, y: number): Foo;
Buzz({ x: number, y: number }): Foo;
// Repeat for other methods
}
However, it would be more efficient to achieve something like this...
class Foo {
constructor(x: number);
constructor(x: number, y: number);
constructor({ x: number, y: number });
// Ideally, I would like a syntax similar to...
Fizz(constructor()): Foo;
Buzz(constructor()): Foo;
}
Is there a simple way to declare and reuse an overloaded method signature?