Commando
Commando

Reputation: 358

How to pass JSON as a prop?

I have a component that gets 2 props, one is another component and the second is a JSON file:

interface HeaderProps {
  title: ReactElement<typeof Title>;
  data: any;
}

The prop 'title' is very specific and shows that only the component <Title /> can be passed there.

How can I specify the type of the prop data so instead of any it will be for JSON type only?

Upvotes: 1

Views: 5492

Answers (2)

Yunhai
Yunhai

Reputation: 1411

I believe you are looking for an "object" with dynamic key of any type.

data: { [key:string]: any }// if array then {[key:string]: any}[]

Upvotes: 2

k-wasilewski
k-wasilewski

Reputation: 4643

JSON in Typescript is just Object:

data: Object

Upvotes: 4

Related Questions