1

I have the following json file with data:

[{
    "sectionKey": "key1",
    "sectionName": "name",
    "content": []
  },
  {
    "sectionKey": "key2",
    "sectionName": "name",
    "content": []
  }
]

My question is: how to declare the content of the array in typescript? like "Type"[]. Please do not suggest using any[] since it removes all type checking provided by TypeScript.

2
  • What are you trying to type, the content[] property? Commented Jan 16, 2022 at 13:18
  • nope. the objects into the array Commented Jan 16, 2022 at 13:19

1 Answer 1

2
interface CoolObject {
  sectionKey: string;
  sectionName: string;
  content: Array<any>;
}

const greatArray: CoolObject[] = [
  {
    sectionKey: 'key1',
    sectionName: 'name',
    content: [],
  },
  {
    sectionKey: 'key2',
    sectionName: 'name',
    content: [],
    foobar: 'something' // not assignable to type 'CoolObject'
  },
];

If it's the 'content' poperty you're trying to typehint you can use Array<YourType> or YourType[] too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.