Jordi Castillo
Jordi Castillo

Reputation: 693

Assign specific type to compatible object

i have the following situation:

I have a function that returns States[] (that is an object of strings) and i want to assign them to an object of strings

interface State {
  name: string;
  adminName1: string;
  countryCode: string;
  countryName: string;
}

const myStates : <{ [id: string]: string}[]> = getStates()

Which returns:

Type 'State[]' is not assignable to type '{ [id: string]: string; }[]'.

(I need that because i have a functions working with { [id: string]: string}[] type )

Upvotes: 0

Views: 35

Answers (1)

Owl
Owl

Reputation: 6853

You need to add index signature to the interface

interface State {
  name: string;
  adminName1: string;
  countryCode: string;
  countryName: string;
  [key: string]: string;
}

Upvotes: 1

Related Questions