我正试图将这个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][]的参数。
最佳答案
如果你不确定类型的话你可以用任何的,
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[] ){ ... }