Absolutely, TypeScript allows you to customize your code as desired.
For instance, you can encapsulate your prop types within a generic interface named DataProps
with the parameter placeholder Datum
(which can represent any type of data).
interface DataProps<Datum> {
items: Array<Datum>
}
Next, if you want to incorporate this property type into a stateless React component class, you can utilize the React.SFC
type for referencing such components without states.
function getDataComponent<Datum>() : React.SFC<DataProps<Datum>>{
return props => <div>
<p>{props.items.toString()}</p>
</div>
}
This function generates a specific category of stateless React component. To use it,
const MyStringDataComponent = getDataComponent<string>();
Now, you have a designated data component type, specifically designed for string data. Then, create an instance by
ReactDOM.render(<MyStringDataComponent items={['a', 'b']} />, document.getElementById('root'))
You should see a,b
displayed on your page.