I’m trying to pass an object via props and having an issue understanding what Typescript wants.
Do I create an interface for Props than a separate interface for the properties within the object?
What I have:
import React from 'react';
interface Props {
link: object;
}
interface link {
description: string;
url: string;
}
const Link = (props: Props) => {
const { link } = props;
return (
<div>
<div>
{ link.description }({ link.url })
</div>
</div>
);
};
export default Link;
Errors I’m getting:
Property 'description' does not exist on type 'object'. TS2339
Property 'url' does not exist on type 'object'. TS2339
>Solution :
use it like this, because Typescript can’t detect that if the object has property or not:
interface Props {
link: {
description: string;
url: string;
};
}
const Link = (props: Props) => {
const { link } = props;
return (
<div>
<div>
{link.description}({link.url})
</div>
</div>
);
};
export default Link;