I am looking to create a class that accepts any object as input, along with additional methods defined within the class.
The properties of the object can be arbitrary and named at the time of constructing the class.
// definition of the desired class type:
// the input object (T) combined with the prototype methods (TestClass)
type MyClass = TestClass & T
If I were to conceptualize this, my approach would be as follows:
class TestClass<T extends Object> {
constructor(obj: T) {
// currently it's not possible to directly assign obj to this
// example properties like this.name or this.wife cannot be accessed
this = obj
}
returnMe(): this & T {
return this
}
}
// The correct type is returned by this function
// however, the actual class lacks the additional types
function TestFactory<T extends Object>(obj: T): TestClass<T> & T {
return new TestClass(obj)
}