Warm-up · Activity 1 of 7
// 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.
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?
Practice · Activity 3 of 7
Fill in the short flag that saves TypeScript as a development dependency of the project.
npm install typescriptPractice · 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); }Practice · Activity 5 of 7
Match each command to what it does.
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);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.tsOutput
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
Hint 1
tsc reports main.ts(3,3): error TS2322: Type 'string' is not assignable to type 'number'. Line 3 returns text.
Hint 2
split gives an array of strings, so version.split(".")[0] is the string "7".
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.tsRun the checks (needs learnrun.js in the same folder):
npx tsc
node --testDownload learnrun.jsExercise 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
Hint 1
tsc reports main.ts(8,35): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
Hint 2
total.toFixed(2) already turns the number into text. formatPrice wants the number and calls toFixed itself.
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.tsRun the checks (needs learnrun.js in the same folder):
npx tsc
node --testDownload learnrun.jsCommon 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.