Below is a code snippet for review.
An error occurs when calling the get
method within the class, but works fine when called outside. Any thoughts on why?
type DefinedKeys<T> = keyof {
[K in keyof T as undefined extends T[K] ? never : K]: K
}
class MyAbstractClass {
get<K extends keyof this & DefinedKeys<this>>(key: K): this[K] {
return this[key];
}
}
class MyClass extends MyAbstractClass {
foo: string = "ok";
optional?: string;
test() {
this.get('foo'); // Doesn't work
}
}
const instance = new MyClass();
const yepItsWorks: string = instance.get("foo"); // Ok