ฉันมักจะรวบรวม typescript กับธง - noImplicitAny สิ่งนี้สมเหตุสมผลแล้วที่ฉันต้องการให้การตรวจสอบประเภทของฉันแน่นที่สุด
ปัญหาของฉันคือด้วยรหัสต่อไปนี้ฉันได้รับข้อผิดพลาดIndex signature of object type implicitly has an 'any' type:
interface ISomeObject {
firstKey: string;
secondKey: string;
thirdKey: string;
}
let someObject: ISomeObject = {
firstKey: 'firstValue',
secondKey: 'secondValue',
thirdKey: 'thirdValue'
};
let key: string = 'secondKey';
let secondValue: string = someObject[key];
สิ่งสำคัญที่ควรทราบคือแนวคิดคือตัวแปรสำคัญมาจากที่อื่นในแอปพลิเคชันและอาจเป็นคีย์ใด ๆ ในวัตถุ
ฉันได้ลองใช้การคัดเลือกนักแสดงโดย:
let secondValue: string = <string>someObject[key];
หรือสถานการณ์ของฉันเป็นไปไม่ได้ด้วย--noImplicitAny?