Reputation: 3249
I import a json file into my TypeScript file and I am not sure how to iterate over it.
The error message states:
Element implicitly has an 'any' type because expression of
type 'string' can't be used to index type...
My Json file
{
"foo": false,
"bar": false,
}
My code:
import * as emails from './test_emails.json'
for (let v in emails) {
emails[v]; <- access is the error here
}
Does anyone know whats going on here? I am wondering how I can make emails[v]
return the boolean value. Any help is highly appreciated!
Upvotes: 0
Views: 1363
Reputation: 724
I would do it in this way
JSON
{
"emails" : [
{
"foo": "false",
"bar": "false",
}
]
}
Typescript
import * as data from './test_emails.json';
public emails: IEmail [] = data.emails; // Make sure to import IEmail
for (let v in emails) {
console.log(v.foo);
console.log(v.bar); // Do whatever you want I just printed on console
}
Interface
export interface IEmail
{
foo : boolean;
bar : boolean;
}
Upvotes: 1