I know how to write React Functional Component and TypeScript + function overloading.
But when these two are combined together, I am confused!
Here is a minimum code example to illustrate what I want to do:
import React, { FunctionComponent } from 'react';
type PropType1 = { type: 'type1'; name: string };
type PropType2 = { type: 'type2'; age: number };
// How do I properly hook FunctionComponent + PropType1 + PropType2?
// It's called FunctionComponent right?
function Example({ type, name, age }) {
if (name === undefined) {
return (
<div>
{type}, {age}
</div>
);
} else {
return (
<div>
{type}, {name}
</div>
);
}
}
export default Example
This example's usage should look like this:
<Example type="type1" name="Joseph"/>
// => <div> type1, Joseph </div>
<Example type="type2" age={18}/>
// => <div> type2, 18 </div>
<Example type="type3" name="Joseph" age={18}/>
// => Compile Error!
How do I properly hook FunctionComponent + PropType1 + PropType2?
And, it's called FunctionComponent right?