TypeScript Get Started
The playgrounds on this site type-check and run TypeScript in your browser, so you can follow the whole course without installing anything. For real projects, though, you'll want TypeScript on your own computer. This lesson shows how.
Installing the compiler
TypeScript is distributed as an npm package. You need Node.js installed first (it includes npm). Then, inside your project folder, install TypeScript as a development dependency:
npm install --save-dev typescript
npx tsc --versionadded 1 package, and audited 2 packages in 2s
found 0 vulnerabilities
Version 5.9.3
Note: Installing per project (instead of globally with
-g) means every teammate and your CI server use exactly the same compiler version, recorded inpackage.json.
Compiling a file
Create a file called hello.ts:
function greet(person: string): string {
return `Hello, ${person}!`;
}
console.log(greet("Linh"));Compile it with tsc and run the result with Node:
npx tsc hello.ts
node hello.jsHello, Linh!
tsc created hello.js next to your source. Open it and you'll see that the type annotations are simply gone:
function greet(person) {
return `Hello, ${person}!`;
}
console.log(greet("Linh"));When there is a type error
Change the call to greet(42) and compile again:
npx tsc hello.tshello.ts:5:19 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
5 console.log(greet(42));
~~
Found 1 error in hello.ts:5
In a terminal tsc prints a nicer format with the offending line underlined. The playgrounds on this site use the compact form hello.ts(5,19): error TS2345: … — same information, one line per error. Try it:
function greet(person: string): string {
return `Hello, ${person}!`;
}
console.log(greet(42));main.ts(5,19): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
Note: By default
tscstill writes the.jsfile even when there are type errors (unless you enablenoEmitOnError). The playgrounds here are stricter: code with errors is never run.
Setting up a project with tsconfig.json
Real projects have many files, so instead of naming each one you create a configuration file:
npx tsc --initCreated a new tsconfig.json
You can learn more at https://aka.ms/tsconfig
A minimal, sensible configuration looks like this:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"strict": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}Now npx tsc (with no arguments) compiles everything in src/ into dist/. Add --watch to recompile automatically every time you save. You'll learn the important options in the tsconfig.json lesson.
Other ways to run TypeScript
| Tool | What it does |
|---|---|
tsc |
The official compiler: type-checks and emits JavaScript |
tsx / ts-node |
Run a .ts file directly in Node during development |
| Node.js 23.6+ | Runs .ts files natively by stripping types (no type check) |
| Vite, esbuild, swc | Bundlers/transpilers that strip types very fast; run tsc --noEmit separately for checking |
| Editors (VS Code) | Use the TypeScript language service to show errors as you type |
Tip: A common setup is: the bundler compiles for speed, and
tsc --noEmitruns in CI to make sure there are no type errors.
Test yourself
Which command installs TypeScript as a project dev dependency?
- npm install typescript --global
- npm install --save-dev typescript
- node install tsc
- npx typescript init
What does npx tsc hello.ts produce?
- A native executable
- hello.js with the types removed
- A tsconfig.json file
- Nothing unless there are errors
Which file lets you run npx tsc without listing every source file?
- package.json
- tsconfig.json
- index.d.ts
- .tsrc