question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to write a clone function?

See original GitHub issue

I would like to scope this only to arrays for now. How do I write a clone function that clones tuples and arrays while preserving types?

I get an array type back with this:

function clone<T>(arr:T): T {
    return [].concat(arr)
}

let cloned = clone([1,2,3])

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thecotnecommented, Aug 18, 2020

// Could not deduce type "(ArrayLike<T> | Iterable<T>) => Array<T>" arising from the arguments unknown,(ArrayLike<T> | Iterable<T>) => Array<T>.
// Please, provide type parameters for the call or provide default type value for parameters
// of "<T>(T, T) => T" function.
// WAT??
const clone01 = arr => Array.from(arr)

// (ArrayLike<T> | Iterable<T>) => Array<T>
// this is same but works
function clone01_1(arr) {
    return Array.from(arr)
}


// <T>(T) => $Immutable<Array<T | unknown>>
// this one looks wrong
const clone02 = arr => [].concat(arr)

try

1reaction
vkurchatkincommented, May 27, 2020

Interestingly, this simply crashes:

function clone<T>(arr: Array<T>): Array<T> {
    const array: Array<T> = Array.from(arr);
    return array;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Clone() method in Java - GeeksforGeeks
Object cloning refers to the creation of an exact copy of an object. It creates a new instance of the class of the...
Read more >
Java Object clone() Method - Cloning in Java - DigitalOcean
The default implementation of Java Object clone() method is using shallow copy. It's using reflection API to create the copy of the instance....
Read more >
Object Cloning in java - Javatpoint
The object cloning in java is a way to create exact copy of an object. For this purpose, clone() method of Object class...
Read more >
Java Cloning - Deep and Shallow Copy - Copy Constructors
The java clone() method provides this functionality. Learn to create shallow copy, deep copy and copy constructors in Java.
Read more >
JavaScript: clone a function - Stack Overflow
Here is an updated answer var newFunc = oldFunc.bind({}); //clones the function with '{}' acting as its new 'this' parameter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found