With current versions of TypeScript, this is an error:
((_)=>{})(...[null]);// ERROR: Expected 1 arguments, but got a minimum of 0.In order to make this code work, the developer needs to tell the type checker to shut up, which could look like this:
(((_)=>{})asany)(...[null]);// Working fine.This is unfortunate, as passing arguments for functions around in tuples is a common pattern in functional programming and there is currently no way to do it safely in TypeScript.
This was mentioned in #5296, which lead to the spread operator being supported for arrays. Supporting tuples in addition to arrays is not a straightforward change, since tuples can currently have additional values, which are not type checked (there is proposal #6229, which aims to fix this):
functionf(s: string,a: number,b: number,c: number, ...rest: number[]){// ...}// legalletquad: [string,number,number,number,number]=["foo",1,2,3,4];f(...quad);// and triplequad=["foo",1,2,3,4,"boo!"];// it is legal currentlyf(...quad);// now it blows up in run time because rest has a stringHere is a longer and more detailed example taken from #5296 showing the desired behavior in detail:
functionf(s: string,a: number,b: number,c: number, ...rest: number[]){// ...}functiong(s: string,a: number,b: number){}// legalletquad: [string,number,number,number,number]=["foo",1,2,3,4];lettriple: [string,number,number,number]=["foo",1,2,3];letdouble: [string,number,number]=["foo",1,2];f(...quad);// and triplef(...double,3);f("foo", ...[1,2,3]);f(...quad,1, ...[1,2,3]);// and tripleg(...double);// illegalf(...double);// too short -- double doesn't match parameter 'c'g(...triple);// too many parametersg(...quad);// too many parameters
With current versions of TypeScript, this is an error:
In order to make this code work, the developer needs to tell the type checker to shut up, which could look like this:
This is unfortunate, as passing arguments for functions around in tuples is a common pattern in functional programming and there is currently no way to do it safely in TypeScript.
This was mentioned in #5296, which lead to the spread operator being supported for arrays. Supporting tuples in addition to arrays is not a straightforward change, since tuples can currently have additional values, which are not type checked (there is proposal #6229, which aims to fix this):
Here is a longer and more detailed example taken from #5296 showing the desired behavior in detail: