Reputation: 139
I'm trying to pass this TypeScript array to a function. I've tried a number of parameter types to make this compile, but none of them seem to work. Here's the array:
var driverTally = [
{ driver: 'driver1', numEscorts: 0},
{ driver: 'driver2', numEscorts: 0},
{ driver: 'driver3', numEscorts: 0}
];
doStuff(driverTally : Array<[string,number]>){ ... }
The compiler keeps saying : "Argument of type '{ driver: string; numEscorts: number; }[]' is not assignable to parameter of type '[string, number][]'.
Upvotes: 2
Views: 88
Reputation: 12005
I recommend you to determine interface:
interface IDriver {
drive: string;
numEscorts: number;
}
Then declare array with initialization:
public drivers: IDriver[] = [];
After you can pass this array as function parameter:
doStuff(driverTally: IDriver[] ){ ... }
Upvotes: 0
Reputation: 222532
You cna use any if you are not sure about the type,
doStuff(driverTally : any){ ... }
or create a Class of the following type
export class Driver {
public string driver;
public int numEscorts;
}
and then declare your array as,
driverstally : Driver[] = [
{ driver: 'driver1', numEscorts: 0},
{ driver: 'driver2', numEscorts: 0},
{ driver: 'driver3', numEscorts: 0}
];
and then pass it as,
doStuff(driverTally :Driver[] ){ ... }
Upvotes: 3