Currently working on programming an internal abstract class for a project, and I need it to be generic in order to make it extendable.
The goal is to have my class named as if it were extending the T template, like Sample extends T
, so that all parameters of T
are included. For example, if T
is Vue, then all Vue parameters such as $el
or $options
should be accessible without needing to re-declare or include Vue again.
Here's what I currently have:
export namespace SDK {
export abstract class Sample<T> {
private static methods: any = {
hello: Sample.prototype.hello
}
abstract callMe(): void;
x: number = 0;
y: number = 0;
width: number = 1;
height: number = 1;
hello(): void {
console.log('Sample hello world');
this.callMe();
}
}
}
However, I'm unsure how to go about including the properties of T into Sample.
I would like it to look something like this:
export namespace SDK {
export abstract class Sample<T> {
private static methods: any = {
hello: Sample.prototype.hello
}
abstract callMe() : void;
x: number = 0;
y: number = 0;
width: number = 1;
height: number = 1;
// T properties (example with Vue)
$el: HTMLElement
...
hello () : void {
console.log('Sample hello world')
this.callMe()
}
}
}
The desired way to use my class would be like this:
export default class MyComponent extends SDK.Sample<Vue> {
name: string = 'my-component';
callMe (): void {
console.log('called')
}
mounted (): void {
this.hello()
}
}
I haven't been able to find any information on how to properly extend from a templated class with parameters included in it.