Monday, April 10, 2017

Tuples in TypeScript/ES6 and C# 7

TypeScript and ES6 has tuple functionality:




C# tuple is more flexible, as aside from that it allows destructuring the tuple to variables, it also supports returning the object as-is:




The named tuple can be easily achieved in TypeScript/ES6 by returning an object instead of array, however, the destructuring to variables using the array syntax won't work anymore:



Would have to use object destructuring syntax:




However, unlike in C#, that won't work in TypeScript/ES6. In TypeScript/ES6, we would have to match the name of the variables to the name of the object's property names that is being destructured from.




But if you really don't like the object's property names given by the author of the function, or it conflicts with your existing variable name; with TypeScript/ES6, you can rename the destructured variable using the following syntax:




Read more:

https://blogs.msdn.microsoft.com/typescript/2014/11/12/announcing-typescript-1-3/

https://strongloop.com/strongblog/getting-started-with-javascript-es6-destructuring/

http://wesbos.com/destructuring-renaming/

http://blog.marcgravell.com/2017/04/exploring-tuples-as-library-author.html


Happy Coding!