DEV Community

Discussion on: A Beginners Guide to using Typescript with React

Collapse
 
petyosi profile image
Petyo Ivanov

My bread and butter with React and TypeScript is to type the functional components with the React.FC generic type. With your example above, that would be something like this:

export const StandardComponent: React.FC<{ title?: string}> = 
({ children, title = 'Dr.'}) => {
 // rest of the code
}

Why? it adds automatically children for you, makes the signature easier to read (IMHO) and checks the return type.

Note: FC is a short-hand for FunctionComponent

Collapse
 
bmcmahen profile image
Ben McMahen

Cool! I've used FunctionComponent in the past but didn't realize it actually added children for you.

Thanks for the tip!

Collapse
 
bsgreenb profile image
Ben Greenberg 🧢

The React TypeScript cheat sheet recommends doing them using the shorter version of Ben's syntax:

type AppProps = { message: string }; /* could also use interface */
const App = ({ message }: AppProps) => {message};

github.com/typescript-cheatsheets/...