Typescript isn't discarding type information here. In the DefinitelyTyped lodash.d.ts file, you can see that cloneDeep
is defined as
cloneDeep<T>(
val: T,
customizer?: (value: any) => any,
thisArg?: any
) : T
Ignoring the arguments we don't care about, it takes a T
as input, and spits out a T
as output. So Typescript isn't losing any type information; it considers the output of cloneDeep
to be the same type as the input.
You should be able to verify this via your editor: assuming you have some editor that lets you inspect the type of variables or autocompletes methods (which I'd highly recommend, if you don't).
Why then is the typeof
not working as you expect? It's because the Typescript type information doesn't carry over to runtime. instanceof
is a native JS operator, which Typescript doesn't change the behavior of, which you can see by running this snippet:
"use strict";
class A {}
let a = new A();
let b = _.cloneDeep(a);
if (b instanceof A) {
alert("b is an instance of A");
} else {
alert("b is not an instance of A");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
The reason that b instanceof A
is false is that instanceof
is checking against constructors: x instanceof A
returns true if the function A
is a constructor somewhere in x's prototype chain (see the MDN documentation on instanceof). Lodash, however, doesn't use constructors when it clones objects. It can't. (How would it know what arguments to pass?) It creates a plain JS object that has all the methods of the cloned object, but doesn't reproduce it's prototype chain.
Lodash's clone
(and most of lodash's methods, really) is best used when dealing with raw JS Objects. If you're using it in conjunction with constructors and instanceof
checking things get a bit murky.
One solution here is to avoid the instanceof
checking, and do something akin to duck typing; don't check that the object's constructor is a particular function, but check that the object has the properties that you expect it to.
Another solution is, as suggested in the comments, implement a clone method on your class itself, which wouldn't use lodash.
class A() {
clone() {
var cloned = new A(); //pass appropriate constructor args
//make other necessary changes to make the state match
return cloned;
}
}