Installing TypeScript
Learn Installing TypeScript
The TypeScript compiler is distributed as the typescript npm package, normally installed in a project as a development dependency.
In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.
What you’ll learn
- Explain the main rule behind installing typescript.
- Predict the result of the complete runnable TypeScript example.
- Recognize one common mistake and correct it.
Understand the concept
From a project directory, run npm init -y and npm install --save-dev typescript. Execute its compiler with npx tsc --version. A project-local installation helps teammates and build systems use the same compiler version, rather than depending on a machine-wide global command.
Terminal setup: npm init -y, then npm install --save-dev typescript, then npx tsc --version.
Run a complete TypeScript example
Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.
TypeScript code
const packageName: string = "typescript";
console.log(`${packageName} compiler available after npm install`);Verified output
typescript compiler available after npm installHow to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.
Trace the execution
The sample prints a reminder; installing the compiler happens in the terminal, not from this TypeScript program. npm downloads the compiler package and npx invokes its command.
Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.
Common mistakes and how to avoid them
Do not confuse npm install typescript with node main.ts. Installing a dependency does not automatically compile your source.
Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.
Practice it yourself
In an empty practice directory run npm init -y, npm install -D typescript and npx tsc --version. Record the version and keep package-lock.json.
Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.
Check your understanding
Review the key idea
Installing TypeScript: From a project directory, run npm init -y and npm install --save-dev typescript.
A versioned project dependency supports repeatable builds.
Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.
Reaching the recap and completing the quizzes are separate milestones.