49

I'm running node.js v17.2.0 and TypeScript v4.5.4. I'm trying to use structuredClone() on a Map, and it doesn't seem to be working. ES2021 is targeted in tsconfig.json, and included in lib. Is this function just plain not available in TypeScript? Is there something else I need to include to get it?

@types/node is also installed, and I've made sure that it works in node.js environment.

structuredClone working in node.js environment

Wubzy
  • 684
  • 1
  • 5
  • 9
  • Seems like you have to call the function: let foo = structuredClone(value). This should help you https://developer.mozilla.org/en-US/docs/Web/API/structuredClone – SARAN SURYA Jan 11 '22 at 02:37
  • 2
    `structuredClone` is not part of ES2021. It's part of the web and node APIs. That's the lib you'll need to include (assuming it has been updated already, given that node 17 is still rather recent). – Bergi Jan 11 '22 at 02:54
  • 4
    `your-package-manager install @types/node@^17`. If your `tsconfig.json` specifies `"types"` under `"compilerOptions"`, make sure `"node"` is listed as in `"types": ["node"]`. – Aluan Haddad Jan 11 '22 at 03:00
  • @SARANSURYA the screenshot was just to prove it was available. – Wubzy Jan 12 '22 at 03:19
  • 2
    @AluanHaddad this should be the solution, thanks – Wubzy Jan 12 '22 at 03:19
  • @AluanHaddad I did that but still doesn't work. I installed the latest version (17.0.21). If I do a file search in my node_modules @types/node folder, there is no reference of "structuredClone" – Vincent V. Feb 24 '22 at 09:47
  • 1
    @VincentV. I've just opened a PR in @types/node to add `structuredClone`: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/59434 – Josh Mar 26 '22 at 23:06

3 Answers3

46

structuredClone is now present in @types/node v17.0.29:

Run the following command to have access to it:

npm i --save-dev @types/node@17.0.29

Joel Carneiro
  • 3,287
  • 3
  • 16
  • 20
Josh
  • 2,324
  • 1
  • 21
  • 29
26

structuredClone will be available in lib.dom of TypeScript v4.7 (as of 2022-05-19 it is currently in beta, but will be out soon). You can see where structuredClone was added to TypeScript here.

If you need to add it to your project temporarily until you can upgrade TypeScript, you can do that by putting the following definitions from the commit linked above into a structuredClone.d.ts file in your project (the base name doesn't matter, but the .d.ts does):

interface WindowOrWorkerGlobalScope {
    structuredClone(value: any, options?: StructuredSerializeOptions): any;
}
declare function structuredClone( value: any, options?: StructuredSerializeOptions): any;

(StructuredSerializeOptions is already defined by lib.dom.d.ts for postMessage, so we don't need to add it.)

Then just remove that file when you've upgraded later.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Bob
  • 416
  • 3
  • 6
1

It was an ackowledged bug in Jest.

Jest supports structuredClone since v28.0.0-alpha.8 which is just above 28.0.0

https://github.com/facebook/jest/issues/12628

jest-preset-angular is nearly at the stage of supporting that jest version. According to the below issue 13.0.0-next-1 does.

https://github.com/thymikee/jest-preset-angular/issues/1774

Árpád Magosányi
  • 1,394
  • 2
  • 19
  • 35