Currently, I am in the process of transferring some logic to an abstract class. Let's look at the abstract generic class with specific constraints:
abstract class AbstractVersion<
TModel extends object,
TProperty extends keyof TModel,
TValue = TModel[TProperty]> {
private _version: TValue;
public get version(): TValue {
return this._version;
}
}
This can then be extended as shown below:
class MyVersionedModel extends AbstractVersion<MyModel, 'MyNumericId'>
Up until now, everything seems to be working fine. But now I'm interested in using my TProperty-type as a value. Is it possible to achieve something like this?
abstract class AbstractVersion<
TModel extends object,
TProperty extends keyof TModel,
TValue = TModel[TProperty]> {
private _version: TValue;
public get version(): TValue {
return this._version;
}
set(model: TModel): void {
this._version = model[TProperty];
}
apply(fx: (property: string, value: TValue) => boolean): boolean {
return fx(TProperty.toString(), this.version);
}
}
It is clear that I am encountering a syntax error
'TProperty' only refers to a type, but is being used as a value here.
on model[TProperty]
and TProperty.toString()
. However, is there a way to access TProperty as a value?