如何在TypeScript接口中定义合并功能?

有一些方法可以为合并函数定义这样的接口:

interface mergeFunc<T, S, P exntends T,S>(t:T, s:S):P;

var merge:mergeFunc = function (t:any, s:any):any {
   var res = {};
   for (let x in t) res[x] = t[x];
   for (let x in s) res[x] = s[x];
   return res;
}

采纳答案:

在TypeScript 1.6(或今天的每晚TypeScript,使用npm install typescript@next )中,您将能够使用交集类型来编写:

declare function mergeFunc<T, S>(t:T, s:S): T & S;

请参阅https://github.com/Microsoft/TypeScript/pull/3622


参考更多解答: How to define in TypeScript interface for merge function? ,转载请保留出处如何在TypeScript接口中定义合并功能?及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:typescript