Setting Up TypeScript and Running Code

TypeScript is a popular superset of JavaScript that introduces static typing and other features to help developers write robust, maintainable code. This guide will walk you through setting up TypeScript, writing your first program, and compiling and running TypeScript code.

Prerequisites

Before starting, ensure the following:

  1. Node.js Installed: Download and install Node.js from nodejs.org. This will also install npm (Node Package Manager).
  2. Code Editor: Install a code editor such as Visual Studio Code, which offers excellent TypeScript support.

Installing TypeScript

TypeScript can be installed globally or locally within a project using npm.

Global Installation

  1. Open a terminal or command prompt.
  2. Run the following command:
  1. Verify the installation:

This command will display the installed TypeScript version.

typescript version

Local Installation (Per Project)

  1. Navigate to your project directory:
  1. Run the following command to install TypeScript as a development dependency:
  1. Verify the local installation by running:

Initializing a TypeScript Project

TypeScript projects typically include a tsconfig.json file, which specifies the compiler options and project configuration.

  1. In your project directory, run:
   tsc --init

  1. This command generates a tsconfig.json file with default settings. You can customize it according to your project’s requirements. For example:
  1. Create the necessary directories:

Place your TypeScript files in the src directory.

Writing a TypeScript File

  1. Create a file named hello.ts in the src directory:
  1. Open hello.ts in your editor and write the following code:

Compiling TypeScript Code

To compile TypeScript into JavaScript:

  1. Run the TypeScript compiler:

This will compile all files included in the tsconfig.json file. The compiled JavaScript files will appear in the dist directory.

Compiling TypeScript Code

  1. To compile a single file without a tsconfig.json file:

Running the Compiled Code

After compiling, you can run the JavaScript file using Node.js:

  1. Run the following command:
  1. The output should be:
typescript hello world

Additional Tips

Watching for Changes

To automatically compile TypeScript files whenever you save changes:

  1. Use the --watch flag:
  1. TypeScript will continuously watch for changes and recompile as needed.

Installing TypeScript Node (ts-node)

To run TypeScript files directly without compiling:

  1. Install ts-node:
  1. Run the TypeScript file directly:
typescript node

TypeScript Debugging in VS Code

  1. Open the project in Visual Studio Code.
  2. Configure the debugger:
  • Go to the Debug tab.
  • Click on create a launch.json file.
  • Select Node.js.
  1. Update the launch.json file to include:
  1. Set breakpoints and start debugging.

Conclusion

You have successfully set up TypeScript and learned how to compile and run TypeScript code. This setup forms the foundation for building robust and scalable applications with TypeScript.

See Also

3 thoughts on “Setting Up TypeScript and Running Code”

Leave a Comment