Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Bootstrap a TypeScript + React project

    Basarat Ali SyedBasarat Ali Syed

    Learn how to setup a TypeScript + React project from scratch. Understand the reason behind every line involved in the configuration allowing you to customize it at will in the future.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: We'll start off with the Baboon's package.json file. We'll go ahead and install all the modules we'll need in one go. These are the general React modules like React, ReactDOM, Webpack, Webpack CLI, Webpack Dev Server, along with TypeScript and TypeScript-specific things like the types for React, ReactDOM, and ts-loader for Webpack.

    Once the installation is complete, let's kick off by wrapping up all the modifications needed for package.json by simply adding two script targets. We add a build script which just invokes Webpack in production mode. We also add a start script which runs with the Webpack Dev Server for live application development using development mode and serves up the public folder.

    Next we'll go ahead and add a webpack.config.js configuration file. First, we specify the application entry point. Up next is the output location for a built bundle.

    Next, we tell Webpack to support ts and tsx file extensions along with the original js extension. Finally, we tell Webpack that for ts and tsx files, it should use ts-loader.

    We'll go ahead and create our basic HTML file in the public folder. This file simply contains a root div where we'll render our React application, and loads the bundle generated from Webpack.

    Next, we'll go ahead and add a tsconfig.json file to set up the TypeScript compiler options. We enable source maps, so we can debug the TypeScript files.

    We'll be trans-filing to CommonJS. We want our JS to be compatible with ES5. For JSX, we'll be using React. We include all the files in the source folder, and disable compile on save for the IDE, is that will be done with Webpack.

    That's it for the configuration. Now let's write some demo code. I'll create a source app.tsx file. We'll simply import React and ReactDOM. Finally, we use ReactDOM to render "Hello world" to our root div.

    Now if we open up the terminal and run our scripts target npm start, it will start up a Webpack Dev Server and serve the public folder up at localhost 8080. If you visit this URL at the browser, you can see our application running.

    If you make an edit to the file, Webpack Dev Server will compile the file on the fly and re-load the browser. When you are ready to deploy your application, you can execute npm run built. This time, a Webpack will compile our code and write the app.js file to disk.

    If we wanted, we could ship the whole public folder to some hosting service provider, as it contains the built file along with index.html.

    To recap, the set-up simply involves three simple things -- package.json for specifying our npm modules and splits tsconfig.json for configuring TypeScript, webpack.config.js to use a Webpack for compiling and running our code, and npm modules in the browser.