I'm encountering an issue while attempting to import a large JSON file into a TypeScript file. Despite the JSON clearly being an array, my IDE is throwing an error message stating
Property 'length' does not exist on type '{}'
. How can I resolve this issue?
Here's the code snippet:
TypeScript file:
import cities from "./cities.json"
const printData = () => {
console.log(cities.length)
}
JSON file:
[
{ "country": "Andorra", "name": "Sant Julià de Lòria" },
{ "country": "Andorra", "name": "Pas de la Casa" },
{ "country": "Andorra", "name": "Ordino" },
{ "country": "Andorra", "name": "les Escaldes" },
{ "country": "Andorra", "name": "la Massana" },
{ "country": "Andorra", "name": "Encamp" },
... + 128K more objects
]
It seems that the issue may be related to the size of the file, and it has been suggested to use a .d.ts
file to declare it. I attempted the following solution:
let cities: {
country: string
name: string
}[]
export declare module "./cities.json" {
cities
}
However, I now need to reference the object twice as shown below:
import cities from "./cities"
const printData = () => {
console.log(cities.cities.length)
}
Any insights on how to effectively address this issue would be greatly appreciated. Thank you!