Conditional Types Example - React Component Props
This lesson shows a real-life usage of conditional types in React.
We'll cover the following...
Detecting a React component
How can we take advantage of conditional types and use them to extract the types of React component properties? Let’s create a conditional type that checks whether a given type is a React component.
type IsReactComponent<T> =
T extends React.ComponentType<any> ? "yes" : "no";
IsReactComponent
takes a type argument T
and checks whether it extends React.ComponentType
. If yes, it returns a "yes"
string literal type. Otherwise, it returns a "no"
string literal type.
Since ...