'Nameless array in Typescript class?
I'm relatively new to Typescript.
I am working through a tutorial and have some code like this:
export class Brushes {
[key: number]: Brush;
}
As far as I can tell, it creates an indexable class so that I can do things like
const brushes: Brushes = {...}
and then
mybrush = brushes[2];
or whatever.
My question is, how do I tell what the total length of this "array class" is, since
mybrush.length;
is undefined?
Or is there a better way to do what I am trying to do?
Solution 1:[1]
I would take a different approach. Instead of creating an "indexable" class, I'd just store instances of the class in an array.
const brush: Brush = {...};
const arrayOfClasses = [];
arrayOfClasses.push(brush);
const myBrush = arrayOfClasses[0];
arrayOfClasses.length; ## 1
Would that be applicable given your use case? Or do you need to use classes for some reason?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jimmy Cerone |
