node.js - 在类型上找不到带有“字符串”类型参数的索引签名
问题描述
我来自移动应用程序开发,对打字稿没有太多经验。如何声明 [string:any] 形式的地图对象?
错误出现在以下行:map[key] = value;
元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引“对象”类型。
在“Object”.ts(7053) 类型上找不到带有“string”类型参数的索引签名
var docRef = db.collection("accidentDetails").doc(documentId);
docRef.get().then(function(doc: any) {
if (doc.exists) {
console.log("Document data:", doc.data());
var map = new Object();
for (let [key, value] of Object.entries(doc.data())) {
map[key] = value;
// console.log(`${key}: ${value}`);
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
} }).catch(function(error: any) {
console.log("Error getting document:", error);
});
解决方案
您通常不想使用new Object()
. 相反,map
像这样定义:
var map: { [key: string]: any } = {}; // A map of string -> anything you like
如果可以的话,最好any
用更具体的东西替换,但这应该可以开始。
推荐阅读
- angular - 使用spring boot app作为资源服务器,google oauth2作为授权服务器
- javascript - 三.js 只有南半球人脸的BackSide
- laravel - 未找到特征“Illuminate\Foundation\Events\Dispatchable”
- office-js - Outlook 插件中 SSO 的可用性
- javascript - 字段名称在 JSON 中,但数据表无法识别?
- java - 在 liferay 中创建新的 portlet pulgin 时出错
- kdb - 正则表达式左加入 KDB
- reactjs - 使用 Typescript 反应 Redux-Thunk
- dart - 如何检查用户日期和时间是否在颤振中设置正确
- php - IMAP 库:使用 lumen 从 Outlook 电子邮件帐户获取所有电子邮件