Skip to content

Commit f3a04eb

Browse files
authored
Merge pull request microsoft#9 from peterblazejewicz/update/readme
📝 Readme small updates and fixes
2 parents 4515e2f + f66eeff commit f3a04eb

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

README.md

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ This walkthrough illustrates how to adopt TypeScript in an existing React/Babel/
44

55
If you are starting a new React project instead of converting one, you can use [this tutorial](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/tutorials/React.md).
66

7-
Adopting TypeScript in any project can be broken down into 2 phases,
8-
* Adding TypeScript compiler (tsc) to your build pipeline.
9-
* Converting JavaScript files into TypeScript files.
7+
Adopting TypeScript in any project can be broken down into 2 phases:
108

11-
# Understand the existing JavaScript project
9+
* Adding TypeScript compiler (tsc) to your build pipeline.
10+
* Converting JavaScript files into TypeScript files.
11+
12+
## Understand the existing JavaScript project
1213

1314
Before we dive into TypeScript adoption, let's take a look at the structure of the TicTacToe app. It contains a few components and looks like below with or without TypeScript.
1415

@@ -18,7 +19,7 @@ Before we dive into TypeScript adoption, let's take a look at the structure of t
1819

1920
As shown in `package.json`, the app already includes React/ReactDOM, Webpack as bundler & task runner, and [babel-loader](https://github.com/babel/babel-loader) Webpack plugin to use Babel for ES6 and JSX transpilation. The project has the below overall layout before we adopt TypeScript:
2021

21-
```
22+
```txt
2223
TicTacToe_JS /
2324
|---- css/ // css style sheets
2425
|---- src/ // source files
@@ -29,49 +30,50 @@ TicTacToe_JS /
2930
|---- restartBtn.jsx // RestartBtn React component
3031
|---- .babelrc // a list of babel presets
3132
|---- index.html // web page for our app
32-
|---- package.json // node package configuration file
33+
|---- package.json // node package configuration file
3334
|---- webpack.config.js // Webpack configuration file
3435
```
3536

36-
# Add TypeScript compiler to build pipeline
37+
## Add TypeScript compiler to build pipeline
3738

38-
## Install dependencies
39+
### Install dependencies
3940

4041
First off, open terminal and `cd` to the `TicTacToe_JS` folder. Install dependencies in `package.json`.
4142

42-
```
43+
```sh
4344
cd TicTacToe_JS
4445
npm install
4546
```
4647

4748
Additionally, install TypeScript (2.3 or higher), [awesome-typescript-loader](https://www.npmjs.com/package/awesome-typescript-loader) and [source-map-loader](https://www.npmjs.com/package/source-map-loader) as dev dependencies if you haven't. awesome-typescript-loader is a Webpack plugin that helps you compile TypeScript code to JavaScript, much like babel-loader for Babel. There are also other alternative loaders for TypeScript, such as [ts-loader](https://github.com/TypeStrong/ts-loader). source-map-loader adds source map support for debugging.
4849

49-
```
50+
```sh
5051
npm install --save-dev typescript awesome-typescript-loader source-map-loader
5152
```
5253

53-
Get the type declaration files (.d.ts files) from [@types](https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/) for any library in use. For this project, we have React and ReactDOM.
54+
Get the type declaration files (.d.ts files) from [@types](https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/) for any library in use. For this project, we have React and ReactDOM.
5455

55-
```
56+
```sh
5657
npm install --save @types/react @types/react-dom
5758
```
5859

5960
If you are using an older version of React/ReacDOM that are incompatible with the latest .d.ts files from @types, you can specify version number for `@types/react` and `@types/react-dom` in `package.json`.
6061

61-
## Configure TypeScript
62+
### Configure TypeScript
6263

63-
Next, configure TypeScript by creating a `tsconfig.json` file in the `TicTacToe_JS` folder, and add,
64+
Next, configure TypeScript by creating a `tsconfig.json` file in the `TicTacToe_JS` folder, and add,
6465

65-
```
66+
```json
6667
{
6768
"compilerOptions": {
6869
"outDir": "./dist/", // path to output directory
6970
"sourceMap": true, // allow sourcemap support
7071
"strictNullChecks": true, // enable strict null checks as a best practice
71-
"module": "es6", // specifiy module code generation
72+
"module": "es6", // specify module code generation
7273
"jsx": "react", // use typescript to transpile jsx to js
7374
"target": "es5", // specify ECMAScript target version
74-
"allowJs": true // allow a partial TypeScript and JavaScript codebase
75+
"allowJs": true // allow a partial TypeScript and JavaScript codebase
76+
7577
},
7678
"include": [
7779
"./src/"
@@ -81,14 +83,14 @@ Next, configure TypeScript by creating a `tsconfig.json` file in the `TicTacToe_
8183

8284
You can edit some of the options or add more based on your own need. See more full [compiler options](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
8385

84-
## Set up build pipeline
86+
### Set up build pipeline
8587

86-
To add TypeScript compilation as part of our build process, you need to modify the Webpack config file `webpack.configure.js`. This section is specific to Webpack. However, if you are using a different task runner (e.g. Gulp) for your React/Babel project, the idea is the same - replace the Babel build step with TypeScript, as TypeScript also offers transpiling to lower ECMAScript versions and JSX transpilation with a shorter build time in most cases. If you wish, you can also keep Babel by adding a TypeScript build step before Babel and feeding its output to Babel.
88+
To add TypeScript compilation as part of our build process, you need to modify the Webpack config file `webpack.configure.js`. This section is specific to Webpack. However, if you are using a different task runner (e.g. Gulp) for your React/Babel project, the idea is the same - replace the Babel build step with TypeScript, as TypeScript also offers transpiling to lower ECMAScript versions and JSX transpilation with a shorter build time in most cases. If you wish, you can also keep Babel by adding a TypeScript build step before Babel and feeding its output to Babel.
8789

8890
Generally, we need to change `webpack.config.js` in a few ways,
8991

9092
1. Expand the module resolution extensions to include `.ts` and `.tsx` files.
91-
2. Replace `babel-loader` with `awesome-typescript-loader`.
93+
2. Replace `babel-loader` with `awesome-typescript-loader`.
9294
3. Add source-map support.
9395

9496
Let's modify `webpack.configure.js` as below,
@@ -108,7 +110,7 @@ module.exports = {
108110
rules: [
109111
// changed from { test: /\.jsx?$/, use: { loader: 'babel-loader' } },
110112
{ test: /\.(t|j)sx?$/, use: { loader: 'awesome-typescript-loader' } },
111-
// addition - add source-map support
113+
// addition - add source-map support
112114
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
113115
]
114116
},
@@ -127,11 +129,13 @@ Note that if you plan to adopt TypeScript in the entry file, you should change `
127129

128130
You now have the build pipeline correctly set up with TypeScript handling the transpilation. Try bundling the app with the following command and then open `index.html` in a browser,
129131

132+
```sh
133+
npx webpack
130134
```
131-
node ./node_modules/webpack/bin/webpack.js
132-
```
133135

134-
# Transition from JS(X) to TS(X)
136+
> We are assuming you are using `npx` addition for `npm` to [execute npm packages directly](http://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner)
137+
138+
## Transition from JS(X) to TS(X)
135139

136140
In this part, we will walk through the following steps progressively,
137141

@@ -141,7 +145,7 @@ In this part, we will walk through the following steps progressively,
141145

142146
While you get the most out of TypeScript by fully adopting it across your codebase, understanding each of the three steps comes in handy as you decide what to do in case you have certain part of your JavaScript codebase you want to leave as-is (think legacy code that no one understands).
143147

144-
## Minimum transition steps
148+
### Minimum transition steps
145149

146150
Let's look at `gameStateBar.jsx` as an example.
147151

@@ -153,21 +157,21 @@ On line 3 `export class GameStateBar extends React.Component {`, change the clas
153157

154158
By now, awesome-typescript-loader should be able to successfully compile this TypeScript component to JavaScript. Again, try bundling the app with the following command and then open `index.html` in a browser,
155159

156-
```
157-
node ./node_modules/webpack/bin/webpack.js
160+
```sh
161+
npx webpack
158162
```
159163

160-
## Add types
164+
### Add types
161165

162166
The more type information provided to TypeScript, the more powerful its type checking is. As a best practice, we recommend providing types for all declarations. We will again use the `gameStateBar` component as an example.
163167

164-
For any `React.Component`, we should properly define the types of the property and state object. The `gameStateBar` component has no properties, therefore we can use `{}` as type.
168+
For any `React.Component`, we should properly define the types of the property and state object. The `gameStateBar` component has no properties, therefore we can use `{}` as type.
165169

166170
The state object contains only one property `gameState` which shows the game status (either nothing, someone wins, or draw). Given `gameState` can only have certain known string literal values, let's use [string literal type](https://www.typescriptlang.org/docs/handbook/advanced-types.html) and define the interface as follow before the class declaration.
167171

168172
```ts
169173
interface GameStateBarState {
170-
gameState: "" | "X Wins!" | "O Wins!" | "Draw";
174+
gameState: "" | "X Wins!" | "O Wins!" | "Draw";
171175
}
172176
```
173177

@@ -209,19 +213,19 @@ private handleRestart(e: Event) {...}
209213

210214
Again, try bundling the app with the following command and then open `index.html` in a browser,
211215

212-
```
213-
node ./node_modules/webpack/bin/webpack.js
216+
```sh
217+
npx webpack
214218
```
215219

216-
## Adopt TypeScript in the entire codebase
220+
### Adopt TypeScript in the entire codebase
217221

218222
Adopting TypeScript in the entire codebase is more or less repeating the previous two steps for all js(x) files. You may need to make changes additional to what is mentioned above while converting perfectly valid JavaScript to TypeScript. However the TypeScript compiler and your editor (if it has TypeScript support) should give you useful tips and error messages. For instance, parameters can be optional in JavaScript, but in TypeScript all [optional parameter](https://www.typescriptlang.org/docs/handbook/functions.html) must be marked with `?`
219223

220224
You can see the fully converted TicTacToe project in the `TicTacToe_TS` folder. Build the app with,
221225

222-
```
226+
```sh
223227
npm install
224-
node ./node_modules/webpack/bin/webpack.js
228+
npx webpack
225229
```
226230

227231
Run the app by opening `index.html`.

0 commit comments

Comments
 (0)