Extracting this suggestion from this issue:
#1336
Currently the variable arguments list supports variable arguments only as the last argument to the function:
functionfoo(arg1: number, ...arg2: string[]){}This compiles to the following javascript:
functionfoo(arg1){vararg2=[];for(var_i=1;_i<arguments.length;_i++){arg2[_i-1]=arguments[_i];}}However, variable argument functions are limited to appearing only as the last argument and not the first argument. I propose support be added for having a variable argument appear first, followed by one or more fixed arguments:
functionsubscribe(...events: string[],callback: (message: string)=>void){}// the following would compilesubscribe(message=>alert(message));// gets all messagessubscribe('errorMessages',message=>alert(message));subscribe('errorMessages','customMessageTypeFoo123',(message: string)=>{alert(message);});// the following would not compilesubscribe();// supplied parameters do not match any signature of call targetsubscribe('a1');// argument of type 'string' does not match parameter of type '(message: string) => void'subscribe('a1','a2');// argument of type 'string' does not match parameter of type '(message: string) => void'subscribe compiles to the following JavaScript:
functionsubscribe(){varevents=[];varcallback=arguments[arguments.length-1];for(var_i=0;_i<arguments.length-2;_i++){events[_i]=arguments[_i];}}notes: it should be impossible for typescript code to call this function with zero arguments when typechecking. If JS or untyped TS code calls it without arguments, callback will be undefined. However, the same is true of fixed arguments at the beginning of the function.
edit: used a more realistic/motivating example for the fixed-last/variable-arguments-first function.
Extracting this suggestion from this issue:
#1336
Currently the variable arguments list supports variable arguments only as the last argument to the function:
This compiles to the following javascript:
However, variable argument functions are limited to appearing only as the last argument and not the first argument. I propose support be added for having a variable argument appear first, followed by one or more fixed arguments:
subscribe compiles to the following JavaScript:
notes: it should be impossible for typescript code to call this function with zero arguments when typechecking. If JS or untyped TS code calls it without arguments, callback will be undefined. However, the same is true of fixed arguments at the beginning of the function.
edit: used a more realistic/motivating example for the fixed-last/variable-arguments-first function.