Answer the question
In order to leave comments, you need to log in
How to describe an array containing an array of instances from a class in typescript?
Good day to all, I’m just entering TypeScript, I seem to understand the base, but I still can’t understand and google more complex moments.
Let's say I have a class
class TableSelection {
static selectedClass: string = 'selected'
group: object[]
constructor() {
this.group = []
}
select($cell) {
this.clear()
this.group.push($cell)
$cell.addClass(TableSelection.selectedClass)
}
clear() {
this.group.forEach(($selectedCell) => $selectedCell.removeClass(TableSelection.selectedClass))
this.group = []
}
}
Answer the question
In order to leave comments, you need to log in
Well, if it's just like a textbook, then you need to describe the interface that the class will depend on TableSelection
(one class should not directly depend on another, since it is more difficult to maintain).
For example like this:
interface Cell {
addClass(cssClass: string) : void
removeClass(cssClass: string) : void
}
class TableSelection {
...
group
is an array of anything that matches the interfaceCell
interface Cell {
addClass(cssClass: string) : void
removeClass(cssClass: string) : void
}
class TableSelection {
static selectedClass: string = 'selected'
group: Cell[]
constructor() {
this.group = []
}
select($cell: Cell) {
this.clear()
this.group.push($cell)
$cell.addClass(TableSelection.selectedClass)
}
clear() {
this.group.forEach(($selectedCell) => $selectedCell.removeClass(TableSelection.selectedClass))
this.group = []
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question