1

I have the following class and a subclass

export class FlightInfoModel {
    carrierName?: string;
    originCode?: string;
    destCode?: string;
    flightDetails: FlightSegmentEntity[];

    constructor() {
        this.carrierName = "";
        this.originCode = "";
        this.destCode = "";
        this.flightDetails = new Array<FlightSegmentEntity>();
    }
}

export class FlightSegmentEntity {
  CarrierCode: string;
  FlightNo: string;
  Aircraft: string;

  OriginCode: string;
  DestinationCode: string;

  DepartureDate: string;
  ArrivalDate: string;

  FlightDuration: number;
  StopOverDuration: string;

  constructor() {
    this.CarrierCode = "";
    this.FlightNo = "";
    this.Aircraft = "";
    this.OriginCode = "";
    this.DestinationCode = "";
    this.DepartureDate = "";
    this.ArrivalDate = "";
    this.FlightDuration = -1;
    this.StopOverDuration = "";
  }

I am using Typescript and UnderscoreJS to write the following function. flight is the FlightInfoModel object and flightDetails is the FlightSegmentEntity[]

_.reduce(flight.flightDetails,
function(memo, seg){ return memo + seg.FlightDuration + seg.StopOverDuration; },
0)

The underscore function accepts an array collection but I keep getting the following error

error TS2345: Argument of type 'FlightSegmentEntity[]' is not assignable to parameter of type 'Dictionary<{}>'. Index signature is missing in type 'FlightSegmentEntity[]'.

I have read a lot about the contextual typing that Typescript requires and tried doing that but to no avail. BTW I am using Typescript 2.3.4

lohiarahul
  • 1,432
  • 3
  • 22
  • 35

1 Answers1

0

The code that you pasted is missing parseInt(seg.StopOverDuration):

const total = _.reduce(flight.flightDetails, (t, seg) => {
    return t + (seg.FlightDuration + parseInt(seg.StopOverDuration));
}, 0);

But the error is about dictionaries:

Argument of type 'FlightSegmentEntity[]' is not assignable to parameter of type 'Dictionary<{}>'.

You don't have a Dictionary<T> in the pasted code so my guess is that you need to share more details.

Update

I tested the following and works without errors:

import * as _ from "underscore";

class FlightInfoModel {

    public carrierName: string;
    public originCode: string;
    public destCode: string;
    public flightDetails: FlightSegmentEntity[];

    public constructor() {
        this.carrierName = "";
        this.originCode = "";
        this.destCode = "";
        this.flightDetails = new Array<FlightSegmentEntity>();
    }
}

class FlightSegmentEntity {
  public CarrierCode: string;
  public FlightNo: string;
  public Aircraft: string;

  public OriginCode: string;
  public DestinationCode: string;

  public DepartureDate: string;
  public ArrivalDate: string;

  public FlightDuration: number;
  public StopOverDuration: string;

  public constructor() {
    this.CarrierCode = "";
    this.FlightNo = "";
    this.Aircraft = "";
    this.OriginCode = "";
    this.DestinationCode = "";
    this.DepartureDate = "";
    this.ArrivalDate = "";
    this.FlightDuration = -1;
    this.StopOverDuration = "";
  }
}

const flight = new FlightInfoModel();

const result = _.reduce(flight.flightDetails, (memo, seg) => {
        return memo + seg.FlightDuration + parseInt(seg.StopOverDuration);
    },
    0
);

alert(result);
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93