TypeScript Error: Property '0' is missing in type

rkt picture rkt · Jun 9, 2017 · Viewed 25.5k times · Source

I have my interface like this

export interface Details {
  Name: [{
    First: string;
    Last: string;
  }];
}   

I have an observable config variable:

Configuration: KnockoutObservable<Details> = ko.observable<Details>();

and I would like to assign it a value in the constructor as follows:

config = {
  Name: [{
    First: "ABC",
    Last: "DEF"
  },
  {
    First: "LMN",
    Last: "XYZ"
  }]
};

this.Configuration(config);

and I am getting an error:

Types of property 'Name' is incompatible and property '0' is missing in type.
Type '{ First:string; Last:string; }[]' is not assignable to 
type '[{ First: string; Last:string; }]'

I don't have control on changing the interface as it is being used elsewhere.
What is the correct way to initialize this config variable ?

Thanks in advance.

Answer

RyanA91 picture RyanA91 · Sep 26, 2017

I came across the same issue and got around it by changing the interface to:

    interface Details {
        Name: {
            First: string;
            Last: string;
        }[];
    }

I know you may not want the interface changed but hope this helps for anyone that is in this situation.