I know how to write React Functional Component and TypeScript + function overloading.
But when these two are bined 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?
I know how to write React Functional Component and TypeScript + function overloading.
But when these two are bined 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?
Share Improve this question asked May 20, 2019 at 2:57 JosephJoseph 4,7658 gold badges40 silver badges80 bronze badges3 Answers
Reset to default 2You can try Union Types and check the props type in ponent.
import React, { FunctionComponent } from 'react';
type PropType1 = { type: 'type1'; name: string };
type PropType2 = { type: 'type2'; age: number };
function Example(props: PropType1 | PropType2) {
if (props.type === 'type1') {
return (
<div>
{props.type}, {props.name}
</div>
);
} else {
return (
<div>
{props.type}, {props.age}
</div>
);
}
}
export default Example;
Usage
<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: Type '"type3"' is not assignable to type '"type1" | "type2"'
<Example type="type1" age={18}/>
// => Compile Error
Demo
Try to use FC
, functional ponent, with Union Type.
FC
type provides some extra properties, e.g. displayName
, defaultProps
and so on, to make your functional ponent more type safe.
Union
type can provide similar functionality of function overloading. It limits the bination of properties, which choice either PropType1
or PropType2
const Example: FC<PropType1 | PropType2> = (props) => {
if (props.type === 'type1') {
return (
<div>
{props.type}, {props.name}
</div>
);
} else {
return (
<div>
{props.type}, {props.age}
</div>
);
}
}
I solved it by myself
import React, {
PropsWithChildren,
ReactElement
} from 'react';
type PropType1 = { type: 'type1'; name: string };
type PropType2 = { type: 'type2'; age: number };
function Example(props: PropsWithChildren<PropType1>): ReactElement | null;
function Example(props: PropsWithChildren<PropType2>): ReactElement | null;
function Example(props: {
type: 'type1' | 'type2';
name?: string;
age?: number
}) {
const {type, name, age} = props
if (name === undefined) {
return (
<div>
{type}, {age}
</div>
);
} else {
return (
<div>
{type}, {name}
</div>
);
}
}
export default Example