TExcelTAD
Home › TypeScript › TS Get Started

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:

bash · try it yourself
npm install --save-dev typescript
npx tsc --version
added 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 in package.json.

Compiling a file

Create a file called hello.ts:

ts
function greet(person: string): string {
  return `Hello, ${person}!`;
}

console.log(greet("Linh"));

Compile it with tsc and run the result with Node:

bash · try it yourself
npx tsc hello.ts
node hello.js
Hello, Linh!

tsc created hello.js next to your source. Open it and you'll see that the type annotations are simply gone:

js
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:

bash · try it yourself
npx tsc hello.ts
hello.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:

ts · try it yourself
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 tsc still writes the .js file even when there are type errors (unless you enable noEmitOnError). 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:

bash · try it yourself
npx tsc --init
Created a new tsconfig.json

You can learn more at https://aka.ms/tsconfig

A minimal, sensible configuration looks like this:

json
{
  "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 --noEmit runs in CI to make sure there are no type errors.

Test yourself

Which command installs TypeScript as a project dev dependency?

  1. npm install typescript --global
  2. npm install --save-dev typescript
  3. node install tsc
  4. npx typescript init

What does npx tsc hello.ts produce?

  1. A native executable
  2. hello.js with the types removed
  3. A tsconfig.json file
  4. Nothing unless there are errors

Which file lets you run npx tsc without listing every source file?

  1. package.json
  2. tsconfig.json
  3. index.d.ts
  4. .tsrc