[英]Unexpected `Property 'forEach' does not exist on type` error
I am creating chess game and am getting Typescript errors that I don't understand.我正在创建国际象棋游戏并收到 Typescript 我不明白的错误。
The errors occur in this class method:错误发生在这个 class 方法中:
clickEvent (e: MouseEvent): void {
const coordinates: ClientRect = this.chessBoard.getBoundingClientRect();
const xCoord: number = e.clientX - coordinates.left;
const yCoord: number = e.clientY - coordinates.top;
const findColumn: number = Math.trunc(xCoord / this.squareSize);
const findRow: number = Math.trunc(yCoord / this.squareSize);
for (const figure of this.pieces) {
if (figure.row === findRow && figure.column === findColumn) {
this.chessBoard.draggable = true;
this.chessBoard.ondragstart = (e) => e.dataTransfer?.setDragImage(figure.image, this.squareSize / 2, this.squareSize / 2);
this.chessBoard.ondrag = (e) => {
figure.newSteps?.forEach((step: IStep) => {
step.item = this.pieces.find(f => f.row === step.row && f.column === step.column);
(step.item && step.item.color !== figure.color) || !step.item ? this.highlight(step.row, step.column) : null;
});
this.newStepsArr = figure.steps || [];
}
this.chessBoard.ondragend = (e) => {
this.newSquare(figure.row, figure.column);
const squareCoords: ClientRect = this.chessBoard.getBoundingClientRect();
const dragCoordX: number = Math.trunc((e.clientX - squareCoords.left) / this.squareSize);
const dragCoordY: number = Math.trunc((e.clientY - squareCoords.top) / this.squareSize);
figure.row = dragCoordY;
figure.column = dragCoordX;
const correctStep: IStep = figure.steps?.find((step: IStep) => step.row === figure.row && step.column === figure.column);
if (!correctStep || (correctStep.item && correctStep.item.color === figure.color)) {
figure.row = figure.steps[0].row;
figure.column = figure.steps[0].column;
}
this.newStepsArr && this.newStepsArr.slice(1).forEach((step: IStep) => this.newSquare(step.row, step.column, step.item && step.item.image));
figure.newSteps(figure.row, figure.column);
this.newSquare(figure.row, figure.column, figure.image);
this.chessBoard.draggable = false;
}
}
}
}
Here is the relevant interface declaration:这是相关的接口声明:
export interface IPiece {
id: number,
column: number,
row: number,
color: TColors,
name: TName,
image?: HTMLImageElement,
steps?: IStep[],
make?(row: number, column: number): void,
newSteps?(row: number, column: number): void,
}
And these are the errors I am getting:这些是我得到的错误:
figure.newSteps?.forEach - Property 'forEach' does not exist on type '(row: number, column: number) => void'. ts(2339)
figure.row = figure.steps[0].row; - Object is possibly 'undefined'. ts(2532)
Will be so thankfull for your help!非常感谢您的帮助!
There errors are telling you exactly what's wrong!有错误告诉你到底出了什么问题! Listen to them!
听他们说!
figure.newSteps?.forEach - Property 'forEach' does not exist on type '(row: number, column: number) => void'. ts(2339)
According to your IPiece
interface, newSteps
is a function type with the signature (row: number, column: number): void
.根据您的
IPiece
接口, newSteps
是一个 function 类型,带有签名(row: number, column: number): void
。 Functions do not have a forEach
method (unless you add one).函数没有
forEach
方法(除非您添加一个)。 Obviously you are referring to the forEach defined for Arrays .显然,您指的是为 Arrays 定义的forEach 。 So you either have a basic logic problem or a typo, eg perhaps you meant:
因此,您要么有基本的逻辑问题,要么有错字,例如,您的意思可能是:
figure.steps?.forEach
because IPIece.steps
is an Array.因为
IPIece.steps
是一个数组。
Likewise, the second error is correct as well.同样,第二个错误也是正确的。 On the line
在线上
figure.row = figure.steps[0].row;
both figure.steps
could be undefined
(you declared steps
in IPiece
with a ?
) and figure.steps[0]
could be undefined
as well (eg if it is an empty array). figure.steps
都可以是undefined
的(您在IPiece
中用?
声明了steps
)和figure.steps[0]
也可以是undefined
的(例如,如果它是一个空数组)。 You have not checked for either condition prior to this line.您尚未检查此行之前的任何一种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.