Skip to content
aviral gupta

// B1.2 · ~30 min · Beginner

Installing TypeScript 7.0 and running tsc

After this lesson you can install TypeScript 7.0 in a project, check which tsc it runs, and type-check a project of several files with npx tsc.

Lesson 2 of 5 in B1 Setup and the basics

You will be able to

  • Install TypeScript 7.0 with npm and check which version npx tsc runs
  • Run npx tsc on a project and tell from its output which file and line to fix
  • Import between files so that npx tsc reports no errors and node main.ts runs
  1. Warm-up · Activity 1 of 7

    Warm-up from npm: you run npm install -D typescript in a project folder that has a package.json. Which of these happen? Pick all that apply.

    Select all that apply.

  2. Predict · Activity 2 of 7

    Predict: today (30 September 2026) you run npm install -D typescript in a new project, then npx tsc --version. What does it print?

  3. Practice · Activity 3 of 7

    Fill in the short flag that saves TypeScript as a development dependency of the project.

    npm install typescript
  4. Practice · Activity 4 of 7

    main.ts holds import {half} from "./math.ts"; and, on line 3, console.log(half(9));. This is math.ts. What does npx tsc report?

    export function half(n: number): number {
      const result = n / 2;
      return result.toFixed(1);
    }
  5. Practice · Activity 5 of 7

    Match each command to what it does.

  6. Brain teaser · Activity 6 of 7

    Brain teaser. The folder holds package.json, the course tsconfig.json and this main.ts. npx tsc finds no errors. Which new files are in the folder afterwards?

    const greeting: string = "Hello";
    console.log(greeting);
  7. Apply · Activity 7 of 7

    Mini-task on your computer (it needs Node.js and the network). Make a folder, add a package.json with {"type": "module"}, and run npm install -D typescript. Check npx tsc --version. Copy the course tsconfig.json from the last lesson into the folder, write a main.ts that prints something, and run npx tsc, then node main.ts. Then break a type on purpose and run npx tsc again.

    Check your work against this list

Build it yourself

Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.

Worked example

A project of two files

main.ts imports formatPrice from format.ts, adds up three prices and prints the total with and without 19% tax. Save both files next to the course tsconfig.json and run npx tsc: it checks both files and prints nothing. Then run node main.ts. Try calling formatPrice(total) without the currency and run npx tsc again: the error names main.ts, where the call is.

main.ts

// main.ts imports a function from format.ts; npx tsc checks both files.
import {formatPrice} from "./format.ts";

const prices: number[] = [3.5, 4.25, 2];
let total = 0;
for (const price of prices) {
  total += price;
}

console.log("items:", prices.length);
console.log("total:", formatPrice(total, "EUR"));
console.log("with 19% tax:", formatPrice(total * 1.19, "EUR"));

format.ts

export function formatPrice(amount: number, currency: string): string {
  return amount.toFixed(2) + " " + currency;
}

Run it with

npx tsc
node main.ts

Output

items: 3
total: 9.75 EUR
with 19% tax: 11.60 EUR
  • The import names the file with its extension, ./format.ts, which is the file node main.ts loads.
  • export makes formatPrice visible to other files; without it the import would fail.
  • tsc checks the call in main.ts against the parameters declared in format.ts, across the two files.
  • npx tsc prints nothing for a project without errors, and with the course tsconfig it writes no .js files.
Change it and run it

Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.

The first run downloads the TypeScript compiler (up to 2.1 MB) and keeps it cached. Your code stays on your device.

Exercises

Exercise 1 of 2

Read the version tsc prints

npx tsc --version prints text such as Version 7.0.2. majorVersion should take that text and return the major version as a number: 7. It returns the text "7" instead, and tsc reports it. Fix the return line so that it returns a number. Run the tests: the first one is the type-check.

Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.

The first run downloads the TypeScript compiler (up to 2.1 MB) and keeps it cached. Your code stays on your device.

Hints
  1. Hint 1

    tsc reports main.ts(3,3): error TS2322: Type 'string' is not assignable to type 'number'. Line 3 returns text.

  2. Hint 2

    split gives an array of strings, so version.split(".")[0] is the string "7".

  3. Hint 3

    Turn the text into a number before returning it: return Number(version.split(".")[0]);

Show a solution

One way to solve it. Yours can look different and still pass the checks.

export function majorVersion(output: string): number {
  const version = output.replace("Version ", "");
  return Number(version.split(".")[0]);
}
Run it on your computer

Install TypeScript 7.0 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.

main.ts

export function majorVersion(output: string): number {
  const version = output.replace("Version ", "");
  return version.split(".")[0];
}

main.test.ts

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {majorVersion} from './main.ts';

test('Version 7.0.2 gives 7', () => {
  assert.equal(majorVersion('Version 7.0.2'), 7, 'majorVersion("Version 7.0.2") should be the number 7');
});

test('Version 6.0.3 gives 6', () => {
  assert.equal(majorVersion('Version 6.0.3'), 6, 'majorVersion("Version 6.0.3") should be the number 6');
});

test('Version 10.1.0 gives 10', () => {
  assert.equal(majorVersion('Version 10.1.0'), 10, 'majorVersion("Version 10.1.0") should be the number 10');
});

package.json

{
  "type": "module"
}

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "lib": [
      "esnext",
      "dom"
    ],
    "types": [],
    "strict": true,
    "noEmit": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "allowImportingTsExtensions": true
  },
  "include": [
    "**/*.ts"
  ],
  "exclude": [
    "**/*.test.ts"
  ]
}

npx tsc needs TypeScript in the folder: run npm install --save-dev typescript there once. tsc checks the types; Node.js runs main.ts by removing them.

Run the program:

npx tsc
node main.ts

Run the checks (needs learnrun.js in the same folder):

npx tsc
node --test
Download learnrun.js

Exercise 2 of 2

Call a function from another file

format.ts (next to main.ts, leave it as it is) exports formatPrice(amount: number, currency: string). main.ts should print Total: 9.75 EUR, but tsc reports an error and node main.ts stops with TypeError: amount.toFixed is not a function. Read the diagnostic, then fix the call in main.ts.

Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.

The first run downloads the TypeScript compiler (up to 2.1 MB) and keeps it cached. Your code stays on your device.

Hints
  1. Hint 1

    tsc reports main.ts(8,35): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

  2. Hint 2

    total.toFixed(2) already turns the number into text. formatPrice wants the number and calls toFixed itself.

  3. Hint 3

    Pass total itself: formatPrice(total, "EUR").

Show a solution

One way to solve it. Yours can look different and still pass the checks.

import {formatPrice} from "./format.ts";

const prices = [3.5, 4.25, 2];
let total = 0;
for (const price of prices) {
  total += price;
}
console.log("Total:", formatPrice(total, "EUR"));
Run it on your computer

Install TypeScript 7.0 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.

main.ts

import {formatPrice} from "./format.ts";

const prices = [3.5, 4.25, 2];
let total = 0;
for (const price of prices) {
  total += price;
}
console.log("Total:", formatPrice(total.toFixed(2), "EUR"));

main.test.ts

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';

test('the program prints Total: 9.75 EUR', async () => {
  const out = (await runMain()).trimEnd();
  assert.equal(out, 'Total: 9.75 EUR', `the program printed ${JSON.stringify(out)}`);
});

format.ts

export function formatPrice(amount: number, currency: string): string {
  return amount.toFixed(2) + " " + currency;
}

package.json

{
  "type": "module"
}

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "lib": [
      "esnext",
      "dom"
    ],
    "types": [],
    "strict": true,
    "noEmit": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "allowImportingTsExtensions": true
  },
  "include": [
    "**/*.ts"
  ],
  "exclude": [
    "**/*.test.ts"
  ]
}

npx tsc needs TypeScript in the folder: run npm install --save-dev typescript there once. tsc checks the types; Node.js runs main.ts by removing them.

Run the program:

npx tsc
node main.ts

Run the checks (needs learnrun.js in the same folder):

npx tsc
node --test
Download learnrun.js

Common mistakes

An import without the file extension

import {formatPrice} from "./format";

console.log(formatPrice(9.75, "EUR"));

What tsc or Node.js prints

main.ts(1,27): error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './format.js'?

Why, and the fix

In an ES module, Node.js loads exactly the path you write, and there is no file called format. tsc suggests ./format.js, the name the file would have after compiling to JavaScript. In this course Node.js runs the .ts files themselves, so write "./format.ts"; the course tsconfig allows that extension. With ./format.js, tsc is satisfied but node main.ts stops, because no format.js exists.

The error is in the imported file

import {formatPrice} from "./format.ts";

console.log(formatPrice(9.75, "eur"));

What tsc or Node.js prints

format.ts(2,45): error TS2551: Property 'toUppercase' does not exist on type 'string'. Did you mean 'toUpperCase'?

Why, and the fix

npx tsc checks every file in the project, so the diagnostic can name a file other than main.ts. Read the file name first: here the typo is in format.ts, line 2, column 45. Fix it there, as toUpperCase. main.ts is fine and needs no change.

Leaving out an argument

import {formatPrice} from "./format.ts";

console.log(formatPrice(9.75));

What tsc or Node.js prints

main.ts(3,13): error TS2554: Expected 2 arguments, but got 1.

Why, and the fix

formatPrice declares two parameters, amount and currency, and neither is optional, so every call must pass both. In plain JavaScript the call runs, and currency is undefined: node main.ts prints 9.75 undefined. tsc checks each call against the function declared in the other file. Pass the currency: formatPrice(9.75, "EUR").

TypeScript in the browser: the TypeScript 6.0.3 compiler, Apache-2.0, then your browser’s own engine. Licence and source

Exit ticket

5 questions, no hints. Score 80% or more to complete the lesson.

Finish every activity above to unlock the exit ticket.

Report a problem

Spotted something wrong or unclear? Say what, and it will be checked and fixed.

#

At least 20 characters.

Only if you want a reply.

Key ideas

Install TypeScript into the project

TypeScript is an npm package called typescript, so you need Node.js, which comes with npm. In the project folder, run npm install -D typescript. -D, short for --save-dev, records it under devDependencies in package.json: a tool you need while you write the code, not a library the program loads. npm puts the tsc command inside the project, and npx tsc runs that copy. npx tsc --version prints Version 7.0.2, the latest release on 30 September 2026. A global install, npm install -g typescript, works too, but the TypeScript site recommends one per project: each project keeps its own version, on every machine.

TypeScript 7.0: a native tsc

TypeScript 7.0 is a native port of the compiler, written in Go. It is made to type-check like TypeScript 6.0 and to take the same command-line options, and on a full build it is typically 8 to 12 times faster. You install and run it as before, and it keeps the defaults 6.0 introduced, such as strict being on, which the next lesson covers. What 7.0 does not have yet is a programmatic API, so a tool that loads the compiler from its own code, such as typescript-eslint, still needs 6.0 beside it. The editor on this page checks with TypeScript 6.0.3, which reports the same diagnostics for every program in this lesson.

Run tsc on a whole project

npx tsc with no file names looks for tsconfig.json in the current folder, then in each parent folder, and checks every file the tsconfig includes and every file they import. So main.ts and the format.ts it imports are both checked, and an error can be reported in either. With the course tsconfig it writes no files; silence means no errors. If it finds no tsconfig.json at all, tsc prints its help text and checks nothing. An import names the file with its extension, import {formatPrice} from "./format.ts", because node main.ts loads exactly that file. Then run the program: npx tsc, then node main.ts, as two commands.

Sources

Last reviewed September 30, 2026