Is there any way (special syntax) to apply something like rest
parameters to templates in TypeScript?
If question is unclear then take a look at below example and comments.
Question is - can I apply here something like rest ...Types
:
function mixIt<A, B> (a: A, b: B): A & B;
function mixIt<A, B, C>(a: A, b: B, c: C): A & B & C;
/*
* ??
* If I would like to add D, E, F, etc, do I have to overwrite it like above?
*/
function mixIt<A, B, C>(...args: any[]): any{
let mixin = <A & B & C> {};
args.forEach( obj => {
for(let key in obj) {
if( ! mixin.hasOwnProperty(key) ) {
(<any>mixin)[key] = obj[key];
}
}
});
return mixin;
}
FYI - error detection is as expected:
class X {x: number = 7;}
class Y {y: string = 'ok';}
class Z {z: boolean = false;}
let x = new X;
let y = new Y;
let z = new Z;
let xy = mixIt(x, y);
let xyz = mixIt(x, y, z);
xyz.z; // Ok;
xy.z; // Error - as expected. VS Code editor also highlights it