Typescript 类型脚本如何向声明的对象添加新属性?

Typescript 类型脚本如何向声明的对象添加新属性?,typescript,Typescript,我创建了一个对象: countryDetails:Object = {}; 将属性添加到此对象时,如: this.countryDetails.countryLongName = details.obj.long_name; this.countryDetails.countryShortName = details.obj.short_name; 我得到一个错误,因为: “类型”对象上不存在属性countryShortName 我同意,没有关于countryShortName的声明。但是在

我创建了一个对象:

countryDetails:Object = {};
将属性添加到此对象时,如:

this.countryDetails.countryLongName = details.obj.long_name;
this.countryDetails.countryShortName = details.obj.short_name;
我得到一个错误,因为: “类型”对象上不存在属性countryShortName


我同意,没有关于countryShortName的声明。但是在Typescript中,将此值或将来的值添加到对象中的正确方法是什么?

如注释中所述,最好使用数据接口

例如:

export interface CountryDetails {
  countryLongName: string;
  countryShortName: string;
  // other properties
}
然后声明正确类型的变量

countryDetails: CountryDetails
this.countryDetails.countryLongName = details.obj.long_name;
this.countryDetails.countryShortName = details.obj.short_name;

键入
any
可能是您想要的,而不是
Object
我是否应该使用任何..?是:
countryDetails:any={}我强烈建议使用适当的接口that@bugs你能告诉我使用界面的正确方法吗?非常感激。。