Object
clone

It deeply clones the given value.

Syntax

import { clone } from '@opentf/std';
 
clone<T>(val: T): T;

If you need more types to clone with transferable objects, please use the native structuredClone (opens in a new tab) global function.

Examples

clone(); // undefined
 
clone(undefined); // undefined
 
clone(null); // null
 
clone(true); // true
 
clone(false); // false
 
clone('abc'); // 'abc'
 
clone(1); // 1
 
clone(1n); // 1n
 
input = new Date();
output = clone(input);
input === output; // false
 
input = [1, 2, 3];
output = clone(input);
input === output; // false
 
input = { x: 0, y: 1 };
output = clone(input);
input === output; // false
input.y === output.y; // true
 
input = { 
  arr: [1, 2, 3], 
  obj: { a: 'abc', b: 123 },
  date: new Date(),
  map: new Map([['a', 1], ['b', 2]]) 
};
output = clone(input);
input === output; // false
output.arr === input.arr; // false
output.obj === input.obj; // false
output.map === input.map; // false

Try