Clone
@ts-rust/std / Types / Clone
Interface: Clone<T>
Defined in: packages/std/src/types.ts:97
Defines a type T
that is cloneable, providing a method to create a new,
independent instance of itself.
The Clone interface ensures that any type implementing it can produce
a duplicate of its current instance via the clone
method. The type T
represents the specific type of the implementing instance.
Similar to Rust's Clone
trait, this interface is intended for types that
need explicit duplication logic (e.g., objects or complex structures),
as opposed to Primitive types, which are implicitly copied by value.
Unlike reference types that might share state, a clone
implementation should produce a distinct instance, though the depth of the copy
(shallow or deep) is left to the implementor.
T
is the type of the instance that implements this interface.
Typically, this is the class or type itself (e.g., a class MyType
would
implement Clone<MyType>
).
Example
class MyType implements Clone<MyType> {
constructor(public value: number) {}
clone(this: MyType): MyType {
return new MyType(this.value);
}
}
const original = new MyType(42);
const duplicate = original.clone();
expect(duplicate.value).toBe(original.value); // same value
expect(duplicate).not.toBe(original); // different reference
Type Parameters
Type Parameter |
---|
|
Methods
clone()
Call Signature
clone(this): T;
Defined in: packages/std/src/types.ts:98
Parameters
Parameter | Type |
---|---|
|
|
Returns
T
Call Signature
clone(this): T;
Defined in: packages/std/src/types.ts:99
Parameters
Parameter | Type |
---|---|
|
|
Returns
T