In the following piece of TypeScript definition code, I'd like to reuse the function type of baz for the baz property of the Foo interface.
declare module 'foo' {
/* Not all exports will be reused in the interface. */
export function bar(a: string): string
/* Imagine a big and verbose function
definition with plenty overloadings: */
export function baz(a: number): number
export function baz(a: Overloaded): number
interface Foo {
/* The interface is for a function, so I cannot use module */
(a: string): string
/* how do I reuse the full definition of the baz function? */
baz
}
export default Foo
}
I have not been able to find a way to reuse the definition other than copy-paste. Is there a better way than copy-paste? It's alright if I have to define the interface first and reuse its members as static exports.
Type of baz can be reused with type computation (typeof and | in this case):
// Foo will contain 2 overloads of baz
type Foo = { (a: string): string; } | typeof baz;
However note that a type is somehow different to a interface. e.g. it can not be used in class .. implements ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With