1
在下面的一段TypeScript定义代码中,我想重复使用baz
的函数类型作为Foo
接口的baz
属性。TypeScript:重新使用接口定义中的函数定义
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
}
我一直没有找到一种方法来重用复制粘贴以外的定义。有没有比复制粘贴更好的方法?如果我必须首先定义接口并将其成员用作静态导出,那就没问题了。
// Foo will contain 2 overloads of baz
type Foo = { (a: string): string; } | typeof baz;
然而注意,type
是将interface
有些不同:
谢谢! typeof是答案。它应该是这样回答我的特定问题,虽然:'接口Foo = {(a:字符串):字符串; baz:typeof baz}'。 – Avaq