四种不同的功能签名,用一种父类型封装

时间:2017-04-19 18:33:44

标签: node.js typescript2.0 typescript2.2

我有这四种不同的功能签名:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="container">
  <div id="inner"></div>
</div>

如何创建父类型,这4种类型从哪种类型继承?这样我就可以在函数签名中使用该父类型,而不是:

type BeforeHookCallbackMode = (h: IBeforeHook) => void;
type BeforeHookRegularMode = (h?: IBeforeHook) => Promise<any>;
type BeforeHookObservableMode = (h?: IBeforeHook) => Observable<any>;
type BeforeHookEEMode = (h?: IBeforeHook) => EventEmitter;

1 个答案:

答案 0 :(得分:1)

你不是在找父母&#34;类型,但是&#34; union&#34;类型。

type BeforeHookMode = BeforeHookCallbackMode | 
  BeforeHookRegularMode | 
  BeforeHookObservableMode | 
  BeforeHookEEMode;

function (fn: BeforeHookMode) {