We have created the following type according to this post : https://stackoverflow.com/a/49752227
export type KeysOfType<T, TProp> = { [P in keyof T]: T[P] extends TProp? P : never}[keyof T];
In this example
class A {
public prop1: number;
public prop2: string;
public prop3: string;
}
class B<O extends A>
{
private prop: KeysOfType<O, string>;
private prop2: KeysOfType<A, string>;
public method(): void
{
let o: O;
let notTypedAsString = o[this.prop];
let a: A;
let typedAsString = a[this.prop2];
}
}
The expression o[this.prop]
is not typed as string despite the fact that this.prop
is typed as a "string" property of O.
Link to the playground
Is there a way to make it work ?