I'm trying to write a unwrap method of a class that holds a value, or an instance of itself with an inner value:
class Identity<T> {
private readonly value: T;
constructor(value: T) {
this.value = value;
}
static of<T>(value: T): Identity<T> {
return new Identity(value);
}
join(): T { // which return type should go here?
if (this.value instanceof Identity) {
return this.value.join();
}
return this.value;
}
}
Here is the test:
const one = Identity.of(Identity.of(Identity.of(123)));
const result: number = one.join();
expect(result).toEqual(123);
Thanks you folks!!