gpt4 book ai didi

javascript - 减少数组函数的 typescript 错误 - "string' 不可分配给类型的参数

转载 作者:行者123 更新时间:2023-12-04 03:41:14 25 4
gpt4 key购买 nike

我目前正在学习 typescript 并遇到了以下函数的 typescript 错误:

export interface WebspaceBackupArtifact {
readonly date?: string;
readonly name?: string | null;
readonly path?: string | null;
readonly type?: string | null;
}

interface WebspaceBackupArtifactExtended extends WebspaceBackupArtifact {
size: string;
type: string;
}

const selected = [] as WebspaceBackupArtifactExtended[],

const totalSize = (): number => {
return selected
.filter(
(artifact: WebspaceBackupArtifactExtended) =>
artifact.type !== 'directory',
)
.reduce(
(accumulator: number, artifact: WebspaceBackupArtifactExtended) =>
accumulator + artifact.size,
0,
);
}

上述函数产生以下错误:

ERROR in /app/src/views/myfile.vue(394,11): No overload matches thiscall. Overload 1 of 3, '(callbackfn: (previousValue:WebspaceBackupArtifactExtended, currentValue:WebspaceBackupArtifactExtended, currentIndex: number, array:WebspaceBackupArtifactExtended[]) => WebspaceBackupArtifactExtended,initialValue: WebspaceBackupArtifactExtended):WebspaceBackupArtifactExtended', gave the following error. my-project| Argument of type '(accumulator: number, artifact:WebspaceBackupArtifactExtended) => string' is not assignable toparameter of type '(previousValue: WebspaceBackupArtifactExtended,currentValue: WebspaceBackupArtifactExtended, currentIndex: number,array: WebspaceBackupArtifactExtended[]) =>WebspaceBackupArtifactExtended'. my-project | Types ofparameters 'accumulator' and 'previousValue' are incompatible.my-project | Type 'WebspaceBackupArtifactExtended' is notassignable to type 'number'.

my-project | Overload 2 of 3, '(callbackfn: (previousValue:number, currentValue: WebspaceBackupArtifactExtended, currentIndex:number, array: WebspaceBackupArtifactExtended[]) => number,initialValue: number): number', gave the following error. my-project
| Argument of type '(accumulator: number, artifact:WebspaceBackupArtifactExtended) => string' is not assignable toparameter of type '(previousValue: number, currentValue:WebspaceBackupArtifactExtended, currentIndex: number, array:WebspaceBackupArtifactExtended[]) => number'. my-project |
Type 'string' is not assignable to type 'number'. my-project |
392 | ) my-project | 393 | .reduce( my-project| > 394 | (accumulator: number, artifact:WebspaceBackupArtifactExtended) => my-project | |
^ my-project | 395 | accumulator + artifact.size,my-project | 396 | 0, my-project | 397 |
);

为什么在我定义了类型之后它会抛出这些错误?我尝试更改此行:

(累加器:数字,工件:WebspaceBackupArtifactExtended)=>

(previousValue:WebspaceBackupArtifactExtended,currentValue:WebspaceBackupArtifactExtended)=>

但它仍然导致相同的错误消息...

最佳答案

您将 WebspaceBackupArtifactExtended.size 定义为 number 因此 accumulator + artifact.size 返回一个字符串。将 size 的定义更改为 number 可以修复错误。

关于javascript - 减少数组函数的 typescript 错误 - "string' 不可分配给类型的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65995118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com