gpt4 book ai didi

typescript - typescript 参数不正确

转载 作者:搜寻专家 更新时间:2023-10-30 21:27:43 26 4
gpt4 key购买 nike

我正在尝试将此 TypeScript 数组传递给一个函数。我已经尝试了多种参数类型来进行编译,但它们似乎都不起作用。这是数组:

var driverTally = [
{ driver: 'driver1', numEscorts: 0},
{ driver: 'driver2', numEscorts: 0},
{ driver: 'driver3', numEscorts: 0}
];

doStuff(driverTally : Array<[string,number]>){ ... }

编译器一直说:“'{ driver: string; numEscorts: number; }[]' 类型的参数不能分配给 '[string, number][]' 类型的参数。

最佳答案

如果不确定类型,可以使用 any,

doStuff(driverTally : any){ ... }

或创建以下类型的类

export class Driver {
public string driver;
public int numEscorts;
}

然后声明你的数组,

driverstally : Driver[] = [
{ driver: 'driver1', numEscorts: 0},
{ driver: 'driver2', numEscorts: 0},
{ driver: 'driver3', numEscorts: 0}
];

然后将其传递为

doStuff(driverTally :Driver[] ){ ... }

关于typescript - typescript 参数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548339/

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