til

Object.assign can be used to add attributes/properties to an element

javascriptbrowser

Instead of creating an element and calling setAttribute multiple times, you can just use Object.assign.

function downloadJson(json: string, fileName: string) {
  const blob = new Blob([json], { type: "application/json" as const });
  const href = URL.createObjectURL(blob);
  const anchor = Object.assign(document.createElement("a"), {
    href,
    download: `${fileName}.json`,
  });
  anchor.click();
}

I learnt this from @jh3yy