我有一个有关城市详细信息的界面,如下所示:
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
};
};
我想定义为这个接口
const countryClimate: CityDetail = {
id: items['id'],
name: items['name'],
current_temperature: items['main']['temp'],
};
但是我遇到了错误
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
有人可以告诉我我犯了什么错误吗?
答案 0 :(得分:1)
我认为您的项目对象是这样的
const items: Item = {
id: any;
name: any;
current_temperature: any;
}
您应该更改项目对象类型或执行以下操作:
const countryClimate: CityDetail = {
id: items['id'] as number,
name: items['name'] as string,
current_temperature: items['main']['temp'] as string,
};