I am working on a scenario where I have a base class containing a method that can be called from derived classes. In this method, you provide the name of a property from the derived class which must be of a specific type. The method then performs an operation on the value of that property. My objective is to specify this specific type (since keyof alone is not enough).
My question is: How can I properly type this situation?
The current approach is not yielding the desired results
type PropertyNamesOfType<T extends {},TPropertyType> = {
[P in keyof T]: TPropertyType extends T[P] ? P : never
}[keyof T]
declare class TypeUsingBoolPropertyOfDerived{
withArgKeyOfTypeBoolean<E extends PropertyNamesOfType<this, boolean>>(arg:E):void;
}
class Test extends TypeUsingBoolPropertyOfDerived{
boolProp:boolean
stringProp:string
try(): void {
this.withArgKeyOfTypeBoolean('boolProp');
//Argument of type 'string' is not assignable to parameter of type 'PropertyNamesOfType<this, boolean>'.
}
}