Sub topic 4
Installing TypeScript in a Project
This chapter assumes that you know JavaScript functions, objects, and modules. You should also be comfortable with a terminal, npm, package.json, and basic Node.js commands. TypeScript is installed with npm, so Node.js and npm must already be available.
Why install TypeScript locally?
TypeScript adds static type checking to JavaScript. Static type checking examines your code before it runs and reports many mistakes early. The TypeScript compiler, named tsc, performs this checking and can also produce JavaScript files.
Install TypeScript as a local development dependency. A development dependency is a package needed to build or test a project, but not usually needed by the running application. A local installation gives each project its own compiler version.
A global installation can make commands convenient, but different projects may then use different TypeScript versions accidentally. That can produce different errors or generated output on different machines.
Create a project and install TypeScript
Create a directory for a small project, initialize npm, and install TypeScript:
mkdir ts-demo
cd ts-demo
npm init -y
npm install --save-dev typescript
npx tsc --versionThe npm init -y command creates a default package.json. This file records the project metadata, commands, and dependencies.
The npm install --save-dev typescript command does three things:
- Downloads TypeScript into the project’s
node_modulesdirectory. - Adds TypeScript to
devDependenciesinpackage.json. - Updates
package-lock.jsonwith the resolved package version and its dependency details.
The lock file helps npm install the same dependency tree on another machine or in continuous integration. Continuous integration, or CI, is an automated service that builds and tests code after changes are submitted.
npx runs an executable from the project’s local dependencies. Therefore, npx tsc --version uses the TypeScript version installed in this project.
Use an npm script
You can run npx tsc directly, but an npm script gives the project a shared command name. Add this script to the scripts object in package.json:
{
"scripts": {
"typecheck": "tsc --noEmit"
}
}Run it with:
npm run typecheckThe --noEmit option tells TypeScript to check the code without writing JavaScript files. This is useful when another tool handles the build output.
You can also create a build script later:
{
"scripts": {
"build": "tsc",
"typecheck": "tsc --noEmit"
}
}The build script will use tsconfig.json when that configuration file exists. You will create and edit that file in the next steps of the tutorial.
What happens on another machine?
Commit package.json and package-lock.json to version control. Do not commit node_modules; it is generated locally and can be recreated.
A new developer or CI job can install the exact locked dependencies with:
npm ci
npx tsc --versionnpm ci is intended for clean, repeatable installs. It expects the lock file to match package.json and normally removes an existing node_modules directory before installing.
The project workflow is therefore:
sequenceDiagram
participant D as Developer
participant N as npm
participant T as Local tsc
D->>N: npm install --save-dev typescript
N-->>D: package.json and package-lock.json
D->>T: npx tsc
T-->>D: Type errors or JavaScript outputAvoiding common installation errors
If npx tsc uses an unexpected version, check that you are in the project directory and that TypeScript appears in devDependencies.
If the shell says that tsc is not recognized, do not immediately install it globally. Try npx tsc or npm run build from the project directory.
If npm ci fails because the lock file is out of sync, run npm install after deliberately reviewing the dependency changes. Then commit the updated lock file.
Exercise
Why is a local TypeScript installation usually better than a global installation?
Show answer
A local installation pins the compiler to the project’s dependency configuration, so developers and CI use the same expected version.