Tuesday, May 22, 2018

What if I want to do something like the spread operator of TypeScript in JavaScript?

Well, you are in for some pain! The TypeScript Playground translated this TypeScript...

let whatever = {
   foo: true,
   bar: 42,
   baz: "Belgium",
   qux: {
      yin: new Date(1974, 8, 24, 6, 0, 0, 0),
      yang: function(input) {
         alert(input);
      }
   }
};
let somethingMore = {
   ...whatever,
   bar: 13
};

 
 

...into this JavaScript:

var __assign = (this && this.__assign) || Object.assign || function(t) {
   for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
         t[p] = s[p];
   }
   return t;
};
var whatever = {
   foo: true,
   bar: 42,
   baz: "Belgium",
   qux: {
      yin: new Date(1974, 8, 24, 6, 0, 0, 0),
      yang: function (input) {
         alert(input);
      }
   }
};
var somethingMore = __assign({}, whatever, { bar: 13 });

 
 

, { bar: 13 } disappears from the JavaScript when bar: 13 is removed from the TypeScript.

No comments:

Post a Comment