Skip to main content

Beginner's Guide to Setting Up TypeScript

Beginner's Guide to Setting Up TypeScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It provides static typing and other features to help developers write better code. This guide will walk you through installing TypeScript, setting up a TypeScript project, and configuring your project using tsconfig.json.


How to Install TypeScript

To start using TypeScript, you need to install it globally on your machine. You can do this using npm (Node Package Manager). Here’s how:

  • Open your terminal or command prompt.
  • Run the following command to install TypeScript globally:
    npm install -g typescript
    This command will install TypeScript and make the tsc (TypeScript compiler) command available globally.
  • Verify the installation by running:
    tsc --version
    You should see the installed TypeScript version number.

Creating and Setting Up a TypeScript Project

Once TypeScript is installed, you can create a new TypeScript project. Follow these steps:

  • Create a new directory for your project and navigate into it:
    mkdir my-typescript-project
    cd my-typescript-project
  • Initialize a new Node.js project:
    npm init -y
    This command creates a package.json file with default settings.
  • Install TypeScript as a development dependency in your project:
    npm install --save-dev typescript
  • Create a new TypeScript file (e.g., index.ts):
    touch index.ts
    You can now start writing TypeScript code in this file.

Configuring Your Project with tsconfig.json

The tsconfig.json file is used to configure TypeScript compiler options for your project. Here’s how to create and configure it:

  • Create a tsconfig.json file in the root of your project:
    touch tsconfig.json
  • Add the following basic configuration to the tsconfig.json file:
    {
      "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "outDir": "./dist"
      },
      "include": ["src/**/*"],
      "exclude": ["node_modules", "**/*.spec.ts"]
    }
    This configuration sets up the TypeScript compiler to target ES6, use the CommonJS module system, enforce strict type-checking, and output compiled files to the dist directory.
  • Create a src directory for your TypeScript source files:
    mkdir src
    Move your index.ts file into the src directory:
    mv index.ts src/
  • To compile your TypeScript files, run:
    tsc
    This command compiles all TypeScript files in the src directory according to the settings in your tsconfig.json file and outputs them to the dist directory.

Conclusion

Setting up TypeScript involves installing it globally, creating a new project, and configuring the TypeScript compiler with tsconfig.json. With these steps, you can start leveraging TypeScript's powerful features to write more robust and maintainable code. Happy coding!

Comments