I made a game of Tic Tac Toe on React with TypeScript instead of JavaScript. You can follow this official tutorial of React to build it with JavaScript. Then you can convert it to TypeScript with this handbook.
Tic Tac Toe is a paper-and-pencil game for two players who take turns marking the spaces in a three-by-three grid with X or O. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.
React is a front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications. It is a popular front-end framework. Many companies require their front-end developers to master it.
TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for the development of large applications and transcompiles to JavaScript. As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs. The TypeScript compiler is itself written in TypeScript and compiled to JavaScript. Anders Hejlsberg, the lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript.
I think TypeScript is better than JavaScript. It is also a good combination with React as a popular front-end framework. Now, let's code it.
Using TypeScript with Create React App
Let's get started by creating a React app with TypeScript.
npx create-react-app my-app --template typescript
Install Yarn CLI by using the command npm install --global yarn. Then, we can start development by using the command yarn start. It will recompile our React app every time it has some changes.
CSS
We focus on two files; index.tsx and App.tsx. Let's create CSS files for them; index.css and App.css.
The .ts is the file extension for TypeScript files. The .tsx is used when we want to embed JSX elements inside the files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
importReactfrom'react'importReactDOMfrom'react-dom'import'./index.css'importAppfrom'./App'importreportWebVitalsfrom'./reportWebVitals'ReactDOM.render(<React.StrictMode><App/></React.StrictMode>,document.getElementById('root'));// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals()
We leave it default for index.tsx. Just make sure it renders the app.
classGameextendsReact.Component<{},GameState>{constructor(props: any){super(props)this.state={history:[{squares: Array(9).fill(""),}],stepNumber: 0,xIsNext: true,}}handleClick(i: number){consthistory=this.state.history.slice(0,this.state.stepNumber+1)constcurrent=history[history.length-1]constsquares=current.squares.slice()if(calculateWinner(squares)||squares[i]){return}squares[i]=this.state.xIsNext?"X":"O"this.setState({history: history.concat([{squares: squares}]),stepNumber: history.length,xIsNext:!this.state.xIsNext,})}jumpTo(step: number){this.setState({stepNumber: step,xIsNext:(step%2)===0,})}render() {consthistory=this.state.historyconstcurrent=history[this.state.stepNumber]constwinner=calculateWinner(current.squares)constmoves=history.map((_,move)=>{constdesc=move?'Go to move #'+move:'Go to game start'return(<likey={move}><buttononClick={()=>this.jumpTo(move)}>{desc}</button></li>)})letstatus: stringif(winner){status='Winner: '+winner}else{status='Next player: '+(this.state.xIsNext?'X':'O')}return(<divclassName="game"><divclassName="game-board"><Boardsquares={current.squares}onClick={(i: number)=>this.handleClick(i)}/></div><divclassName="game-info"><div>{status}</div><ol>{moves}</ol></div></div>)}}constApp=()=>{return<Game/>}
We render the board or game, set history, handle a click, then calculate the winner on every move or every time the square got clicked.
Closing
Mastering React and TypeScript is the best choice to be a Front-End expert. React is so popular even some companies put it on the interview. TypeScript is the future and the best choice to handle large projects or work with many developers because it is the only strongly typed programming language I have known for the front-end.
I suggest you master TypeScript first before going further with its development. At least you had a TypeScript crash course. Then you will be able to master its frameworks such as React and Angular.