Javascript 以编程方式添加对象时如何从对象数组中键入属性
编辑: 尝试将Javascript 以编程方式添加对象时如何从对象数组中键入属性,javascript,typescript,Javascript,Typescript,编辑: 尝试将toLower声明为: type countryListProps = { dial_code: string; code: string | number; # code will accept both string and number name: string; lower?: string; }; 编辑: 尝试将toLower声明为: type countryListProps = { dial
toLower
声明为:
type countryListProps = {
dial_code: string;
code: string | number; # code will accept both string and number
name: string;
lower?: string;
};
编辑:
尝试将toLower
声明为:
type countryListProps = {
dial_code: string;
code: string | number; # code will accept both string and number
name: string;
lower?: string;
};
你只要使用
const toLower: {[key: string]: countryListProps} = {};
而不是
let newArr = countryList.map((i)=>{
return {
...i,
lower:'hello',
}
})
const toLower={}作为countryListProps[];
对于(设i=0;i您只需使用
const toLower: {[key: string]: countryListProps} = {};
而不是
let newArr = countryList.map((i)=>{
return {
...i,
lower:'hello',
}
})
const toLower={}作为countryListProps[];
for(设i=0;ibut键可以是任何东西?否?@swoopy我已更新了countryListProps
类型声明。除了布尔值(我认为您不能在typescript中提供布尔值作为键),您可以提供任何键作为键,包括未定义
和空
,但键可以是任何键?否?@swoopy我已更新了countryListProps
类型声明。除了布尔值(我认为您不能在typescript中提供布尔值作为键),您可以提供任何内容作为键,包括未定义
和空