I am consuming an api that returns nested json. It is dynamic so the keys will often be different, but can only be an object or a string, cannot be an array or number etc.
Here is an example:
const response = {
name: 'Pete',
location: 'London',
age: {
year: '21'
}
}
I have tried type it like this:
type Response = {
[key: string]: string
}
Then in a React component I want to use it like this:
type Response = {
[key: string]: string
}
const Foo = ({ data }: Response) => {
return <pre>{JSON.stringify(data, null, 2)}</pre>
}
This gives me the following error:
Property 'x' does not exist on type 'string'
Can anyone point me in the right direction here?
here is a link to stackblitz - https://stackblitz.com/edit/react-ts-4n7vcy?file=App.tsx