I have a dynamic json, for example:
JavaScript
x
6
1
{
2
"first": "aaa",
3
"second": "bbb",
4
"other": "ttt"
5
}
6
And I created interface for this:
JavaScript
1
4
1
interface IResult {
2
[key: string]: string;
3
}
4
I have two components:
JavaScript
1
15
15
1
function FirstComponent() {
2
const [result, setResult] = useState<IResult>();
3
4
useEffect(() => {
5
setResult(some action);
6
})
7
8
return {
9
<SecondComponent result={result}>
10
}
11
}
12
13
14
function SecondComponent(result: IResult) {}
15
But this return me error:
Type “IResult” is not assignable to type ‘string’.
in line: <SecondComponent result={result}>
What I’m doing wrong?
Advertisement
Answer
When you pass arguments to a component, it is taken as props.
By defining SecondComponent as function SecondComponent(result: IResult) {}
, you are declaring types of props as IResult
and not the type of props.result as IResult
.
Modify your declaration as following:
JavaScript
1
6
1
interface IProps {
2
result: IResult
3
}
4
5
function SecondComponent(props: IProps) {}
6