Guest User

Untitled

a guest
Nov 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // this will currently compile
  2.  
  3. export const foo1 = function(symbols: any, v?: boolean){ // symbols is an optional Array<Symbol> argument
  4.  
  5. if(arguments.length < 2){
  6. v = symbols;
  7. symbols = [];
  8. }
  9.  
  10. const tags = {};
  11. for(let s of symbols){
  12. tags[s] = true;
  13. }
  14.  
  15. return {
  16. value: v,
  17. tags
  18. }
  19.  
  20. };
  21.  
  22.  
  23. // now this could be made so much simpler with some compiler tricks!
  24. // but this won't currently compile
  25.  
  26. export const foo2 = function(...symbols: Array<Symbol>, v: any){
  27.  
  28. const tags = {};
  29. for(let s of symbols){
  30. tags[s] = true;
  31. }
  32.  
  33. return {
  34. value: v,
  35. tags
  36. }
  37.  
  38. };
  39.  
  40. // in the second case, I don't have to check to see if the array exists
  41. // and I no longer need optional paramaters
  42.  
  43.  
  44. // so before, it looks like this:
  45.  
  46.  
  47. foo1([Symbol('one'), Symbol('two')], {
  48.  
  49. });
  50.  
  51. // now it looks like:
  52.  
  53.  
  54. foo2(Symbol('one'), Symbol('two'), {
  55.  
  56. });
Add Comment
Please, Sign In to add comment