I'm a beginner when it comes to TypeScript and I am struggling to find the solution to my question.
In my project, there is a config.json file structured like this:
{
"api": {
"baseUrl": "http://localhost:9000/api",
"list": {
"itemsPerPage": 200
}
},
"map": {
"start": {
"position": [51.505, -0.09],
"zoom": 2
},
"leaflet": {
"minOpacity": 0.3,
"accessToken": "something"
}
}
}
I have imported this config.json into a module.tsx file:
import config from '../config.json';
const mapCenter: [number, number] = config.map.start.position;
The 'position' property in the JSON needs to be treated as a '[number, number]' tuple due to requirements of a library I use. However, I am encountering an error:
TS2322: Type 'number[]' is not assignable to type '[number, number]'. Property '0' is missing in type 'number[]'.
I need assistance on how to correctly cast types in this scenario?
Is it possible to declare the type of my JSON using a 'config.d.ts' file? If so, how can I achieve that?
Thank you for your help.