While verifying a certain spread pattern, I noticed the nested object.assign transpiled code and was wondering if that's necessary. The flattened object.assign produces just the same output.
TypeScript Version: 3.9.2 in REPL
Search Terms:
nested object.assign, object spread
Code
source
let aname = "John"
let xname = ""
let yname = xname
let zname
let active = true
const isDefined = (value: any) => typeof value !== 'undefined'
const result = {
...(aname && { aname }),
...(xname && { xname }),
...(isDefined(yname) && { yname }),
...(isDefined(zname) && { zname }),
...(isDefined(active) && { active })
}
console.log(result)
const result2 = Object.assign({}, (aname && { aname }), (xname && { xname }), (isDefined(yname) && { yname }), (isDefined(zname) && { zname }), (isDefined(active) && { active }));
console.log(result2)
result
"use strict";
let aname = "John";
let xname = "";
let yname = xname;
let zname;
let active = true;
const isDefined = (value) => typeof value !== 'undefined';
const result = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (aname && { aname })), (xname && { xname })), (isDefined(yname) && { yname })), (isDefined(zname) && { zname })), (isDefined(active) && { active }));
console.log(result);
const result2 = Object.assign({}, (aname && { aname }), (xname && { xname }), (isDefined(yname) && { yname }), (isDefined(zname) && { zname }), (isDefined(active) && { active }));
console.log(result2);
output
{aname: "John", yname: "", active: true}
{aname: "John", yname: "", active: true}
Expected behavior:
Actual behavior:
Playground Link:
https://www.typescriptlang.org/play/#code/DYUwLgBAhgdlC2IIF4ICIBSB7AFjNAsAFCiQAeciK6hJ4EAnpUqhQiMaRAF7Of1QAxmACWANxYQwAJwCuHIsUFYYAZ0gjVAERAAzETBAATagAoxUYPIBc0GAwCUKAHxSGABxBZdEC1aQAhMioAOSyMEZ6BsYhxEoq6hDSIKqywJCoAN7EELkQAHSFprDsEABkZRCZdqUAvg4ANDl5hfmmbFQVVRAdSPVNRHkFRZo6+oZGpkzsTl3V01T9zbmtpqNRE6a8M+WV1duLjcvDbevjxsXC4iCze9BXEhD1xLVxRMpqWKD5wFgA5qZkql0g43h9EkC0mAAEzUADyACMAFYgYT5KCqVQiP4wUyZWoNCDFZi7bolQ6E9okuY9En9IlnaKTBY3UnzOmNBnaDYXA6sml8p6ctbc86TISiCS3MkPPoOBwAbjBCS+IB+-0BKSh0IcQA
While verifying a certain spread pattern, I noticed the nested
object.assigntranspiled code and was wondering if that's necessary. The flattenedobject.assignproduces just the same output.TypeScript Version: 3.9.2 in REPL
Search Terms:
nested object.assign, object spread
Code
sourceresultoutputExpected behavior:
Actual behavior:
Playground Link:
https://www.typescriptlang.org/play/#code/DYUwLgBAhgdlC2IIF4ICIBSB7AFjNAsAFCiQAeciK6hJ4EAnpUqhQiMaRAF7Of1QAxmACWANxYQwAJwCuHIsUFYYAZ0gjVAERAAzETBAATagAoxUYPIBc0GAwCUKAHxSGABxBZdEC1aQAhMioAOSyMEZ6BsYhxEoq6hDSIKqywJCoAN7EELkQAHSFprDsEABkZRCZdqUAvg4ANDl5hfmmbFQVVRAdSPVNRHkFRZo6+oZGpkzsTl3V01T9zbmtpqNRE6a8M+WV1duLjcvDbevjxsXC4iCze9BXEhD1xLVxRMpqWKD5wFgA5qZkql0g43h9EkC0mAAEzUADyACMAFYgYT5KCqVQiP4wUyZWoNCDFZi7bolQ6E9okuY9En9IlnaKTBY3UnzOmNBnaDYXA6sml8p6ctbc86TISiCS3MkPPoOBwAbjBCS+IB+-0BKSh0IcQA