Imagine a scenario where we have a basic abstract class that represents a piece in a board game such as chess or checkers.
export abstract class Piece<Tags, Move, Position = Vector2> {
public constructor(public position: Position, public tags = null) {}
public abstract getMoves(board: Board<Tags, Move, Position>): Move[]
}
Furthermore, the Board
class is also an abstract class.
export abstract class Board<Tags, Move, Position = Vector2> {
public constructor(
public size: Position,
public pieces: Piece<Tags, Move, Position>[]
) {}
}
However, when attempting to extend this class, Typescript indicates that the board
parameter in the getMoves
method is of type any
.
class Checker extends Piece<Tags, Move> {
getMoves(board /*← any 😢*/) {
return []
}
}
This raises the question - could there be a misunderstanding about generics or abstract classes?