Introduction to TypeScript Setup
Before writing TypeScript applications, you need a development environment that can install dependencies, check TypeScript code, and run the resulting JavaScript. Setting up TypeScript involves installing Node.js and npm, adding the TypeScript compiler to a project, configuring compiler options, and creating source files.
TypeScript extends JavaScript with static type checking. The TypeScript compiler, commonly invoked using the tsc command, checks your code and can generate JavaScript for a selected runtime environment. Browsers and standard JavaScript runtimes generally execute the resulting JavaScript rather than TypeScript type annotations.
In this guide, you will create a TypeScript project from scratch, compile your first program, configure the compiler, and run the generated JavaScript using Node.js. The same fundamentals are useful when preparing projects for frameworks and larger applications.
Prerequisites
Before installing TypeScript, prepare a development environment with the tools needed to manage dependencies and run JavaScript applications.
Node.js
Node.js provides a JavaScript runtime and allows you to execute JavaScript programs outside a browser. It is commonly used to run TypeScript-related development tools.
npm
npm is a package manager included with standard Node.js installations. It installs project dependencies and provides commands for running development scripts.
Code editor
Use a code editor with TypeScript language support. Visual Studio Code is one option, but other compatible editors can also be used.
You do not need a separate TypeScript runtime for this workflow. The standard approach in this guide is to compile TypeScript into JavaScript and execute that JavaScript with Node.js.
Step 1: Install Node.js and npm
Visit the official Node.js website and download a suitable supported release for your operating system. Follow the installer instructions and make sure the installation adds Node.js and npm to your system path.
Once installation is complete, open a terminal, Command Prompt, or PowerShell window and check the installed versions.
node --version
npm --version
Both commands should display version numbers. If the commands are not recognized, check that the installation completed successfully and that the executable locations are available through your system path. You may need to reopen the terminal after installation.
Keeping Node.js and npm available from the terminal is important because the following steps use npm to install TypeScript and execute project scripts.
Step 2: Create a TypeScript Project
Create a new directory for your application and navigate into it. Keeping your files inside a dedicated project directory makes dependencies, source files, and build output easier to manage.
mkdir typescript-project
cd typescript-project
Initialize an npm project by running:
npm init -y
This creates a basic package.json file containing project metadata. The file will also hold scripts and dependency declarations as your application develops.
A typical starting directory will now contain:
typescript-project/
package.json
You can now install TypeScript locally and begin adding source files to this directory.
Step 3: Install TypeScript
Install TypeScript as a development dependency by running the following command inside your project directory.
npm install --save-dev typescript
The --save-dev option records TypeScript as a development dependency. The compiler is needed for development and builds, but your generated JavaScript generally does not require the TypeScript compiler to execute.
Verify the installation by checking the TypeScript compiler version:
npx tsc --version
The command should display the version of the installed compiler. The npx command normally finds the project's local executable, allowing developers and continuous integration environments to use the version recorded in the project's dependencies.
Installing TypeScript locally is generally preferable to depending on an arbitrary global compiler version because the project can declare its development tools and keep them consistent across environments.
Step 4: Generate the TypeScript Configuration
TypeScript uses a configuration file named tsconfig.json to define compiler options and determine which files belong to the project.
Generate an initial configuration using the compiler:
npx tsc --init
The generated file includes compiler settings that you can review and customize. Configuration options should reflect your application environment, including the JavaScript runtime, module system, language target, and build process.
For this introductory project, you can use the following simplified configuration:
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"moduleResolution": "Node",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"noEmitOnError": true
},
"include": ["src/**/*"]
}
This example uses CommonJS-style modules for a simple Node.js project. Modern ESM applications and framework-based projects may need different module settings. Keep your TypeScript configuration compatible with the package configuration and tools that execute or bundle the generated JavaScript.
The main options have these purposes:
targetspecifies the JavaScript language version to emit.moduledetermines the module format generated by the compiler.rootDiridentifies the source directory used for emitted file structure.outDirspecifies the output directory for generated JavaScript.strictenables a collection of stricter type-checking options.noEmitOnErrorprevents output generation when the compiler reports errors during the build.includeidentifies the source files included in the project.
Step 5: Create Your First TypeScript File
Create a directory named src inside the project, then add a file named app.ts. TypeScript source files typically use the .ts extension.
mkdir src
Add the following code to src/app.ts:
const language: string = "TypeScript";
function greet(name: string): string {
return "Hello, " + name + "!";
}
function addNumbers(
first: number,
second: number
): number {
return first + second;
}
console.log("Learning " + language);
console.log(greet("Developer"));
console.log(addNumbers(10, 20));
This program introduces string and number annotations, typed function parameters, and return types. The compiler checks whether the implementation is compatible with those declarations.
Your project structure should now look like this:
typescript-project/
src/
app.ts
package.json
package-lock.json
tsconfig.json
The node_modules directory will also be present after installing TypeScript. It contains the locally installed package dependencies and is not generally committed to source control.
Step 6: Compile TypeScript into JavaScript
With the source file and compiler configuration in place, run the TypeScript compiler from the project root:
npx tsc
When compilation succeeds, TypeScript generates JavaScript in the configured output directory. With the configuration used in this guide, the generated file is dist/app.js.
The compiled JavaScript will look similar to this:
"use strict";
const language = "TypeScript";
function greet(name) {
return "Hello, " + name + "!";
}
function addNumbers(first, second) {
return first + second;
}
console.log("Learning " + language);
console.log(greet("Developer"));
console.log(addNumbers(10, 20));
Notice that the type annotations are absent from the generated JavaScript. They helped the compiler check the source code but are not included in ordinary JavaScript output.
The compiler may also transform supported syntax according to the configured target. The exact generated output can vary with compiler settings and TypeScript versions.
Step 7: Run the Generated JavaScript
After compilation, execute the generated JavaScript file with Node.js:
node dist/app.js
The output should be similar to:
Learning TypeScript
Hello, Developer!
30
This confirms that the TypeScript source has been compiled and the generated JavaScript executes successfully.
When developing browser applications, the generated JavaScript may instead be loaded by a browser or bundled by a build tool. The important concept remains the same: the TypeScript compiler checks and transforms source code, while a compatible JavaScript environment executes the resulting program.
Step 8: Add Useful npm Scripts
Running long commands manually becomes inconvenient as a project grows. npm scripts provide named commands for common development tasks and make it easier to use consistent workflows across machines.
Update the scripts section of your package.json to include:
{
"scripts": {
"build": "tsc",
"start": "node dist/app.js",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"typescript": "^5.0.0"
}
}
This is an illustrative scripts and dependency fragment, not a complete package.json. Keep the actual TypeScript version installed by npm rather than replacing it with the example version, and preserve the remaining package metadata and dependencies.
You can now use shorter commands:
npm run build
npm start
npm run typecheck
The build script compiles the project, the start script executes the generated entry file, and the typecheck script checks the project without emitting JavaScript. Running these commands from your project root makes local development and continuous integration more predictable.
The compiler's noEmit command-line option is useful for type checking without output. It complements a separate build command when you want the development workflow or CI pipeline to detect type errors independently of code generation.
Step 9: Understand the Difference Between File Compilation and Project Compilation
The TypeScript compiler supports both file-based compilation and project-based compilation. Knowing the difference helps avoid confusion when compiler settings do not appear to take effect.
To compile an individual file, you can run:
npx tsc src/app.ts
When source files are supplied directly on the command line, the compiler uses command-line behavior instead of automatically applying the project's tsconfig.json settings in the same way as a project build.
For project compilation, use:
npx tsc
Running the compiler without specifying input files allows it to find and use the project's tsconfig.json. This is normally the preferred command when building an application with a defined source directory, strict settings, and output configuration.
Step 10: Understand Common TypeScript Compiler Errors
One advantage of TypeScript is that many incompatible values can be detected before runtime. When the compiler reports an error, read the diagnostic carefully and identify which value or function contract does not match.
Consider this example:
let courseCount: number = 10;
// TypeScript error:
// courseCount = "ten";
The variable is explicitly declared as a number, but the assignment attempts to store a string. The correct fix depends on the application's intent: assign a number, or change the declared type if the variable genuinely needs to support multiple value types.
Avoid automatically suppressing compiler errors with any or type assertions. First determine whether the code, the declared type, or the data model should change.
Also remember that a successful type check does not guarantee that the application has no runtime defects. Continue to use tests, error handling, and runtime validation for external data.
Using TypeScript in Existing Framework Projects
The basic compiler workflow is useful for understanding TypeScript, but established frameworks often provide their own project creation and build tools. In these applications, the framework typically manages source files, compiler options, module resolution, development servers, and production output.
For example, an Angular application is normally created and managed with the Angular CLI. A React application may use a framework or build tool that supports TypeScript and integrates type checking into development and production workflows.
When adding TypeScript to an existing application, follow the framework's recommended setup rather than replacing its configuration with the simplified Node.js example used in this guide. Framework-managed projects can have compiler settings and build steps that are essential to the application.
Common TypeScript Setup Mistakes
Skipping the Node.js verification step
If Node.js or npm is unavailable in the terminal, dependency installation and project scripts will not work. Verify both commands before continuing.
Installing TypeScript only globally
A global compiler can differ between machines. A local development dependency helps the project and CI environment use the declared TypeScript version consistently.
Ignoring tsconfig.json
Compiler options affect type checking, module output, and emitted file locations. Review the configuration rather than relying on implicit defaults for every project.
Running the wrong output file
When the project emits JavaScript to a dist directory, execute the generated entry file from that directory rather than attempting to run a TypeScript source path using the standard JavaScript workflow.
Compiling a single file instead of the project
Supplying filenames directly to tsc does not use project configuration in the same way as a project build. Run the compiler from the project root without input filenames when you intend to build using tsconfig.json.
Assuming type checking validates runtime data
Type annotations do not automatically check API responses or other untrusted values. Add runtime validation wherever the application needs to verify incoming data.
TypeScript Setup Checklist
Use this checklist to confirm that your basic development environment is ready.
- Install Node.js and verify that Node.js and npm commands work.
- Create a project directory and initialize package.json.
- Install TypeScript as a development dependency.
- Create or review tsconfig.json.
- Create a source directory and add an app.ts file.
- Compile the project with the TypeScript compiler.
- Confirm that the expected JavaScript output is generated.
- Run the generated JavaScript with the appropriate runtime.
- Add npm scripts for repeatable build and type-checking commands.
- Keep the compiler configuration compatible with your framework, runtime, and build tools.
Conclusion
Setting up TypeScript involves preparing a JavaScript development environment, installing the compiler, defining project settings, writing TypeScript source files, and compiling them into JavaScript. Once these steps are understood, you can use the same principles to build more structured applications.
Start with a small project and verify that the complete workflow works before introducing frameworks or additional build tools. As you gain experience, explore strict compiler options, interfaces, generics, modules, testing, and runtime validation to build reliable and maintainable TypeScript applications.