Could someone provide insight into why attempting to access a class property in this scenario is resulting in the error message
error TS2339: Property 'text' does not exist on type 'T'.
Here is the TypeScript code:
class Base {
readonly name: string = this.constructor.name;
};
class MyClass extends Base {
text: string;
constructor(v: string) {
super();
this.text = v;
}
};
function CreateMyClass<T>(type: new (v: string) => T) {
console.debug('CreateMyClass: type:', type)
const myClass: T = new type('hello world');
console.debug('CreateMyClass: myClass:', myClass);
// console.debug('CreateMyClass: myClass.text:', myClass.text);
}
CreateMyClass(MyClass);
The output is as follows:
CreateMyClass: type: [class MyClass extends Base]
CreateMyClass: myClass: MyClass { name: 'MyClass', text: 'hello world' }
If we uncomment the last console.debug statement, the output displays the error:
test/index.ts:20:59 - error TS2339: Property 'text' does not exist on type 'T'.
20 console.debug('CreateMyClass: myClass.text:', myClass.text);
~~~~