user1687891
user1687891

Reputation: 864

How to define and deal with interface?

I have a interface for city details as follows:

export interface CityDetail {
    [index: number]: {
        id?: number,
        name: string,
        latitude?: string,
        longitude?: string,
        current_temperature?: string,
        current_humidity?: string,
        temp_max?: string,
        temp_min?: string,
        current_wind?: string,
        current_wind_pressure?: string,
        current_weather_condition?: string
    };
};

This interface I want to define as

 const countryClimate: CityDetail = {
                id: items['id'],
                name: items['name'],
                current_temperature: items['main']['temp'],
            };

But I am getting error

Type '{ id: any; name: any; current_temperature: any; }' is not assignable to type 'CityDetail'.
  Object literal may only specify known properties, and 'id' does not exist in type 'CityDetail'.ts

Can anybody tell me what mistake I did?

Upvotes: 0

Views: 32

Answers (1)

mr_alex
mr_alex

Reputation: 234

I think your items object like this

const items: Item = {
    id: any;
    name: any;
    current_temperature: any;
}

You should change item object type or do that:

 const countryClimate: CityDetail = {
     id: items['id'] as number,
     name: items['name'] as string,
     current_temperature: items['main']['temp'] as string,
 };

Upvotes: 1

Related Questions