![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjz7pmNZJ_inEOC2UJPWYdydU5fuPajS2JkPgMWdTNia3V845Krq6JBIPrN3AQ5U_7vuRe39iZNrkWaF2GrCqTyiAv5EuU4-rngTmfM3GX-j3El3peX9YXCLKEVhQYSpi7ILhmLyOz3POjf/s1600/Screen+Shot+2017-04-10+at+11.16.41+AM.png)
C# tuple is more flexible, as aside from that it allows destructuring the tuple to variables, it also supports returning the object as-is:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlEEbrUUNsIYkDv38piqDSKDsDTpUdfq3WVdv3TMpYee69NpXsvahEw9XsBM4JRkxAoacZmPE7LZ-ZjrueMuUAA5GhmALSdAvlxsQi7Zbwm2U5di3iwjkpxCtHSHqic0ZAsBGmdaDEnXEe/s1600/Screen+Shot+2017-04-10+at+11.15.37+AM.png)
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:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik2HnnAnM1Ra4NkEKAdzkrozXnhd9kBjacyecrIf8G5-9zYkDRNZprwQwycF1WZoJe4jO3-8hJyS1m_dwIuddQn-c85l4hxkAdrp_5Q4cPkHzAwCn1fyg8Rhljh9bhxLOp2aZPfV0qYmJC/s1600/Screen+Shot+2017-04-10+at+11.24.03+AM.png)
Would have to use object destructuring syntax:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibXIuBC_Jn3gTJVCJ998JUnx8mX5AMsinzME6XGSAP3YioOargfdsWwdPknsNX3GI8hjgnEpmQ_SydqztyGKK045S_aN4ClgCjtkjlrfekoIdQa8kjo7pV1NP_AFpN_DwVDrqSDWIH0dL2/s1600/Screen+Shot+2017-04-10+at+11.28.14+AM.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVVEW81QFhi88757-1shpiDc9qpddXxI7-royyx8cPcS1t6SsVqHr88kLuF_0nzShHxCuoOVWEphXFt8R-CQ2_2oZkg2HwJBbAGKj-XI1yTuDzCU5BdueFQ1OOFKhHMWb-aY0Zx64PQgco/s1600/Screen+Shot+2017-04-10+at+11.36.54+AM.png)
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:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2pBGJ2FuGRGzHFOw23Bo8gLgZfsjjiembj9S04hyphenhyphenj98Oa50UWx3m5WzXJavHAjDwtyf003C2v1XIUzsqkL5GXcpGk40dkkHv0goGCqVHV3aFgDg29xuQmXNaEHrXNo0suyDO8OJ4oIrGz/s1600/Screen+Shot+2017-04-10+at+11.56.50+AM.png)
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!
No comments:
Post a Comment