मैं प्रतिक्रिया और टाइपस्क्रिप्ट का उपयोग कर रहा हूं और नीचे दिए गए कोड ब्लॉक की अंतिम पंक्ति पर यह त्रुटि प्राप्त कर रहा हूं।
Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '{}'. No index signature with a parameter of type 'number' was found on type '{}'. ts(7053)
लाइन से hemisphereInfo[hemisphere][country] = countryInfo;
(पूर्ण कोड के लिए नीचे देखें)। इसमें [country]
बिट के साथ कोई समस्या है।
मूल रूप से, मैं एक अज्ञात कुंजी जोड़ी को किसी ऑब्जेक्ट में जोड़ने का प्रयास कर रहा हूं जो एक इंटरफ़ेस के साथ टाइप किया गया है।
export interface IHemisphere {
[key: string]: object
north: {
[key: string]: object
};
south: {
[key: string]: object
};
}
const hemisphereInfo: IHemisphere = {
north: {},
south: {},
};
for (const hemisphere in hemisphereInfo) {
const countriesInHemisphere: string[] = this.hemiAPI(hemisphere) as unknown as string[];
for (const country in countriesInHemisphere) {
const countryInfo = this.countryAPI(country);
hemisphereInfo[hemisphere][country] = countryInfo;
}
}
सबसे पहले, country
एक स्ट्रिंग है, न कि एक संख्या जैसा कि यह त्रुटि संदेश में कहता है। दूसरे, मैंने दृढ़ता से टाइप की गई कुंजियों के साथ इस समस्या को हल करने वाले उदाहरण देखे हैं, लेकिन मुझे नहीं पता होगा कि जब वे आएंगे तो वे क्या होंगे, इसलिए मैं इंटरफ़ेस में प्रत्येक को निर्दिष्ट नहीं कर सकता।
1 उत्तर
यहां एक गुच्छा मुद्दे चल रहे हैं।
१) of
और in
के बीच का अंतर
एक सरणी के माध्यम से लूप करते समय, सिंटैक्स का उपयोग करके x of array
आपको चाबियाँ देता है, जबकि x in array
आपको मान देता है। भ्रम से बचने के लिए, array.forEach()
जैसे विशेष फ़ंक्शन का उपयोग करना बेहतर हो सकता है।
2) सभी कुंजियाँ टाइप string
हैं
सरणी कुंजियों के साथ काम करते समय, जो संख्याएं हैं, टाइपस्क्रिप्ट उन्हें string
के रूप में देखता है। एक IHemisphere
टाइपस्क्रिप्ट की चाबियों के साथ काम करते समय भी हमें शाब्दिक 'north'
और 'south'
के बजाय string
मिलता है।
3) अस्पष्ट सूचकांक हस्ताक्षर
IHemisphere
में उत्तर और दक्षिण की कुंजियों का प्रकार string
कुंजियों और object
मानों वाली एक वस्तु है। लेकिन वे अज्ञात कुंजियों के लिए टाइप करते हैं जैसा कि आपके सूचकांक हस्ताक्षर द्वारा वर्णित किया गया है, बस object
है।
समाधान
यहाँ एक संभावित समाधान है। मैंने पते #1 पर country in
से country of
पर स्विच किया, #2 को ठीक करने के लिए अभिकथन hemisphere as keyof IHemisphere
जोड़ा, और #3 को ठीक करने के लिए IHemisphere
से शीर्ष-स्तरीय अनुक्रमणिका हस्ताक्षर हटा दिए।
export interface IHemisphere {
north: {
[key: string]: object
};
south: {
[key: string]: object
};
}
const hemisphereInfo: IHemisphere = {
north: {},
south: {},
};
for (const hemisphere in hemisphereInfo) {
const countriesInHemisphere: string[] = this.hemiAPI(hemisphere) as unknown as string[];
for (const country of countriesInHemisphere) {
const countryInfo = this.countryAPI(country);
hemisphereInfo[hemisphere as keyof IHemisphere][country] = countryInfo;
}
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
typescript
टाइपस्क्रिप्ट जावास्क्रिप्ट का एक टाइप किया हुआ सुपरसेट है जो सादे जावास्क्रिप्ट को संकलित करता है। यह जावास्क्रिप्ट में वैकल्पिक प्रकार, कक्षाएं, इंटरफेस और मॉड्यूल जोड़ता है। यह टैग टाइपस्क्रिप्ट के लिए विशिष्ट प्रश्नों के लिए है। इसका उपयोग सामान्य जावास्क्रिप्ट प्रश्नों के लिए नहीं किया जाता है।