How to create custom clone method for typescript class.

I'm trying to recreate the
Instance.Clone()
Instance.Clone()
method, but cloning a typescript class instance instead of a roblox instance, I just can't figure out how to do it while keeping the methods and functionality the same. I've tried: The spread operator (
{ ...instance }
{ ...instance }
), copy/deepCopy from the object-utils package (
Object.deepCopy(instance)
Object.deepCopy(instance)
)
// class.ts

export default class Foo {
Bar = "x";

constructor(bar: string) {
this.Bar = bar;
}

// Custom clone method
clone(): typeof this {
// Do some cloning logic while keeping all members, methods, and functionality

return NewClone;
}

returnSomething() {
return "Something";
}
}
// class.ts

export default class Foo {
Bar = "x";

constructor(bar: string) {
this.Bar = bar;
}

// Custom clone method
clone(): typeof this {
// Do some cloning logic while keeping all members, methods, and functionality

return NewClone;
}

returnSomething() {
return "Something";
}
}
Intended behavior
// somescript.ts

import Class from "./class.ts";

const Instantiated = new Class("Test");
const Cloned = Instantiated.clone();

print(Instantiated.Bar === Cloned.Bar) // true
print(Instantiated.returnSomething() === Cloned.returnSomething()) // true
// somescript.ts

import Class from "./class.ts";

const Instantiated = new Class("Test");
const Cloned = Instantiated.clone();

print(Instantiated.Bar === Cloned.Bar) // true
print(Instantiated.returnSomething() === Cloned.returnSomething()) // true
Solution:
If anyone has this same issue just use
table.clone(instance)
table.clone(instance)
Thanks bitsplicer...
Jump to solution
9 Replies
𝐉𝐚𝐦 ꨄ
𝐉𝐚𝐦 ꨄOP3w ago
Figured it out! Here's my implementation
class Something {
...

clone(): typeof this {
const clone = {};

Object.entries(this).forEach(([key, value]) => {
Object.assign(clone, { [key as any]: value });
});

const thisMetatable = getmetatable(this) as LuaMetatable<any>;
return setmetatable(clone as typeof this, thisMetatable);
}
}
class Something {
...

clone(): typeof this {
const clone = {};

Object.entries(this).forEach(([key, value]) => {
Object.assign(clone, { [key as any]: value });
});

const thisMetatable = getmetatable(this) as LuaMetatable<any>;
return setmetatable(clone as typeof this, thisMetatable);
}
}
Some tests
const NewInstance = new Something();

NewInstance.x = 0;
NewInstance.something = 1;

const Clone = NewInstance.clone();

print(Clone === NewInstance) // false (intended)
print(Clone.x === NewInstance.x) // true
print(Clone.something() === NewInstance.something()) // true
const NewInstance = new Something();

NewInstance.x = 0;
NewInstance.something = 1;

const Clone = NewInstance.clone();

print(Clone === NewInstance) // false (intended)
print(Clone.x === NewInstance.x) // true
print(Clone.something() === NewInstance.something()) // true
It also works with classes which inherit it! This does require the @rbxts/object-utils package though for the Object namespace
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
𝐉𝐚𝐦 ꨄ
𝐉𝐚𝐦 ꨄOP3w ago
Oh... Oops! MAKES SENSE I overthought it
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
𝐉𝐚𝐦 ꨄ
𝐉𝐚𝐦 ꨄOP3w ago
Thanks 😔
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
𝐉𝐚𝐦 ꨄ
𝐉𝐚𝐦 ꨄOP3w ago
Probably metatable stuff Anyways
Solution
𝐉𝐚𝐦 ꨄ
If anyone has this same issue just use
table.clone(instance)
table.clone(instance)
Thanks bitsplicer
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?