Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以编程方式添加对象时如何从对象数组中键入属性_Javascript_Typescript - Fatal编程技术网

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中提供布尔值作为键),您可以提供任何内容作为键,包括
未定义