Greetings everyone! This is my debut post on Stack Overflow, so I'll do my best to articulate my issue clearly. I am currently working with a JSON file named phrases.JSON which has the following structure:
{
"start": {
"affirmative": [
some strings
],
"interrogative": [
some strings
]
},
"mid": [
some strings
]
}
After importing this JSON file as 'phrases' using
import phrases from '../utils/phrases.json'
, I declared it in modules.d.ts like so:
declare module '*.json' {
const data: any
export default data
}
To streamline my workflow, I crafted an interface for the imported phrases.json file as follows:
interface Phrases {
[key: string]: TypePhrases | string[]
start: TypePhrases
mid: string[]
}
interface TypePhrases {
[key: string]: string[]
affirmative: string[]
interrogative: string[]
}
In one of my classes, I implemented a function:
private getPhrases(position: string | number) {
return phrases[position]
}
When invoking this function within my class, I expect to retrieve the 'start' object when passing the string 'start', or an array of strings when passing 'mid'. For example:
const MID_STRING: string = 'mid'
console.log(this.getPhrases(MID_STRING)[0])
However, I encountered the following error in my return function:
Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type '{ "start": { "affirmative": string[]; "interrogative": string[]; }; "mid": string[]; }'. No index signature with a parameter of type 'string' was found on type '{ "start": { "affirmative": string[]; "interrogative": string[]; }; "mid": string[]; }'.
I would greatly appreciate any assistance in resolving this issue. I've attempted several approaches without success, and I'm now seeking help. Thank you!