DEV Community

Chris Otto
Chris Otto

Posted on • Originally published at chrisotto.dev on

4 1

React Tutorial - Add Typescript

This week I was going through the React tutorial. While going through each phase of the tutorial; going over state, JSX and React Components I wanted to be writing it in Typescript. I’ll go through the tutorial code of what I had to change in order to get it working.

  • Use node to install the typescript dependencies we need:

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

  • Change the function definition of Square, pass (props: any) instead of just (props):
function Square(props: any) {}
Enter fullscreen mode Exit fullscreen mode
  • Modify the Board and Game Component to accept any in the Component definition:
class Board extends React.Component<any, any> {}

class Game extends React.Component<any, any> {}
Enter fullscreen mode Exit fullscreen mode
  • Change the file type to be .tsx instead of .js

Just like that, with a few package installs and a few code changes you’re able to compile the project using typescript instead of normal JS. You will probably want to make other modifications to take advantage of other useful parts of typescript.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay