I would like to create a custom Typescript class where I want to be able to access data via [] like:
const a = new CustomClass(1000 /* size of array */);
a[0] = 1;
console.log(a[0]);
And the class has to save the data to an internal array.
I would like to create a custom Typescript class where I want to be able to access data via [] like:
const a = new CustomClass(1000 /* size of array */);
a[0] = 1;
console.log(a[0]);
And the class has to save the data to an internal array.
Here's one option (I made the array a string array, but you could change to whatever you want, or make it a templated class).
class CustomClass extends Array<string> {
greet() {
return "Hello";
}
}
const cc = new CustomClass()
cc[0] = "first";
cc[1] = "second";
console.log(cc[0]);
console.log(cc[1]);
For a templated class, it would be
class CustomClass<T> extends Array<T>