简体   繁体   English

打字稿:在类型“{}”上找不到带有“字符串”类型参数的索引签名

[英]Typescript: No index signature with a parameter of type 'string' was found on type '{}'

So I have the following object:所以我有以下对象:

{"1": {"name": "spencer", "number": "965756"}}, {"2": {"name": "mary", "number": "5346"}},  {"3": {"name": "john", "number": "1234"}}

This object is the result of a database query and I would like to convert this object into an array, using the array.push() method.此对象是数据库查询的结果,我想使用array.push()方法将此对象转换为数组。 This gives the error:这给出了错误:

error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.

I was so sure that the error occurs, because '{}' would be the empty object in case the database query failed.我非常确定会发生错误,因为如果数据库查询失败, '{}'将是空对象。 So I did a check if the result object ( plainObjectResult ) was empty before converting it into an array.因此,在将结果对象( plainObjectResult )转换为数组之前,我检查了它是否为空。 But the error stays and I don't have any idea why.但错误仍然存​​在,我不知道为什么。

Here is the code:这是代码:

export interface Person {
  name: string;
  number: string;
}

export interface PersonResults {
  [key: string]: Person;
}

// make the Realm database Result Object a plain "normal" object
const plainObjectResult: object = realmToPlainObject(queryResult);

const resultArray: PersonResults[] = [];

if (Object.keys(plainObjectResult).length > 0) {
  for (let person in plainObjectResult) {
    // person: 0, 1, 2, ...
    // plainObjectResult[person]: {"name": "spencer", "number": "965756"}
    resultArray.push({[person]: plainObjectResult[person]}); // above error occurs for "plainObjectResult[person]"
  }
}    

Can someone please help me?有人可以帮帮我吗?

This is INVALID {"1": {"name": "spencer", "number": "965756"}}, {"2": {"name": "mary", "number": "5346"}}, {"3": {"name": "john", "number": "1234"}}这是无效的{"1": {"name": "spencer", "number": "965756"}}, {"2": {"name": "mary", "number": "5346"}}, {"3": {"name": "john", "number": "1234"}}

It should have been { "1": { "name": "spencer", "number": "965756" }, "2": { "name": "mary", "number": "5346" }, "3": { "name": "john", "number": "1234" } }应该是{ "1": { "name": "spencer", "number": "965756" }, "2": { "name": "mary", "number": "5346" }, "3": { "name": "john", "number": "1234" } }

Try this尝试这个

export interface Person {
  name: string;
  number: string;
}

export interface PersonResults {
  [key: string]: Person;
}

// make the Realm database Result Object a plain "normal" object
let plainObjectResult: any = { "1": { "name": "spencer", "number": "965756" }, "2": { "name": "mary", "number": "5346" }, "3": { "name": "john", "number": "1234" } };

const resultArray: PersonResults[] = [];

if (Object.keys(plainObjectResult).length > 0) {
  for (let person in plainObjectResult) {
    let id = '' + person;
    let obj:PersonResults  = {};
    obj[id] = plainObjectResult[person];
    resultArray.push(obj); // above error occurs for "plainObjectResult[person]"
  }
}

console.log('resultArray: ', resultArray)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript: 在类型 '{ "A": string; 上找不到参数类型为 'string' 的索引签名 } - Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } 打字稿错误:在类型“{}”上找不到带有“字符串”类型参数的索引签名 - Typescript error : No index signature with a parameter of type 'string' was found on type '{}' Typescript,在类型“{}”上找不到参数类型为“字符串”的索引签名 - Typescript, No index signature with a parameter of type 'string' was found on type '{}' TypeScript + Angular:找不到带有“字符串”类型参数的索引签名 - TypeScript + Angular: No index signature with a parameter of type 'string' was found 在类型“{}”上找不到带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type '{}' 在类型上找不到带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type 在类型 ' 上找不到具有类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type ' 打字稿 - 没有带有“字符串”类型参数的索引签名 - Typescript - No index signature with a parameter of type 'string' TypeScript - 元素隐式具有“任何”类型 [...] 在类型上未找到具有“字符串”类型参数的索引签名 - TypeScript - Element implicitly has an 'any' type [...] No index signature with a parameter of type 'string' was found on type 未找到带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' was found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM