Skip to content

Hello, TypeScript

The Course
•
20 min study

This course is for someone new to TypeScript who already knows JavaScript. You should be able to write a function with parameters and a return value, loop over an array, read object properties, use if/else, and print with console.log. You do not need intermediate JavaScript or promises. If these are unfamiliar, start with JavaScript beginner. For a readiness check, explain what a function that adds two numbers would return before continuing.

TypeScript adds a checker to JavaScript. A type describes values the code is allowed to use: number, string, boolean, and later shapes of objects. Checking happens before execution. The checker follows assignments and function calls to find some mistakes without running them.

In const minutes: number = 25, : number is a type annotation. It describes minutes; it does not convert a string into a number. The compiler checks the source, then produces JavaScript with the annotation removed. JavaScript still does the arithmetic.

Our editors run locally in your browser. Check types shows genuine TypeScript compiler diagnostics. Check & run checks first and runs only when there are no errors. The first check loads the compiler and can take a moment. Each editor is a separate program; definitions from another box are not shared. They have console output, not a webpage DOM. Reset restores the example. Download saves your current source as main.ts. Edits last for this page visit; download work you want to keep before leaving.

The course uses strict checks. A clean check means the program meets the rules the checker can see. It does not prove a calculation is correct, a website response is trustworthy, or a number is positive. You will practice those distinctions all week.

Bring your JavaScript skills

You do not need to finish JavaScript intermediate first. These beginner lessons supply the JavaScript used here:

The JavaScript example below uses no TypeScript annotations yet. It runs in the same editor because ordinary JavaScript syntax is valid TypeScript too. Here the checker can infer the record properties from the local data. New TypeScript syntax—unions, generics and type guards—will be introduced before you need it.

Bridge from JavaScriptTypeScript
const sessions = [
  { topic: "Types", minutes: 25, done: true },
  { topic: "DOM", minutes: 40, done: false }
];
let completedMinutes = 0;

for (const session of sessions) {
  if (session.done) {
    completedMinutes += session.minutes;
  }
}

console.log(completedMinutes);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
25

The loop visits actual records, so you do not have to assume that index 0 exists. Later we will teach safe indexed access explicitly.

Exercise

Readiness check: minutes still to study

The bridge above added up the finished work. Now answer the opposite question: print the minutes still to study (sessions that are not done), then on a second line how many sessions are done. For the supplied records that is 40 and 2. Then change all done fields to true and predict both lines. Do not modify the records while counting. This uses JavaScript syntax only; the checker infers the types from the initial values.

Readiness workspaceTypeScript
const sessions = [
  { topic: "Types", minutes: 25, done: true },
  { topic: "DOM", minutes: 40, done: false },
  { topic: "Loops", minutes: 10, done: true }
];
let remainingMinutes = 0;
let doneCount = 0;

// Loop over sessions: add unfinished minutes, count finished sessions.
console.log(remainingMinutes);
console.log(doneCount);

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Every record goes to exactly one of the two counters, so one decision per record is enough.

Hint 2: a first step

Use for/of to name the current session, then an if/else on session.done inside the loop.

Show one solution
Readiness solutionTypeScript
const sessions = [
  { topic: "Types", minutes: 25, done: true },
  { topic: "DOM", minutes: 40, done: false },
  { topic: "Loops", minutes: 10, done: true }
];
let remainingMinutes = 0;
let doneCount = 0;

for (const session of sessions) {
  if (session.done) {
    doneCount += 1;
  } else {
    remainingMinutes += session.minutes;
  }
}

console.log(remainingMinutes);
console.log(doneCount);

Enable JavaScript to edit and type-check this example.

Text
40
2

Only DOM is unfinished, so remainingMinutes is 40; Types and Loops are done, so doneCount is 2. With every done field true, the program prints 0 and 3. The loop, condition and accumulators are JavaScript. No new annotation is needed for the checker to follow these locally created values. If the algorithm was unfamiliar, revisit the linked JavaScript lessons before continuing.

Predict, check, then run

Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.

Predict, then runTypeScript
const minutes: number = 25;
const sessions: number = 2;

console.log(minutes * sessions);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
50

Read a real compiler error

Click Check types. Read the line number and the two types in the message. Do not remove the annotation just to silence the mismatch.

Find and fix the type errorTypeScript
const minutes: number = "25";

console.log(minutes);

Enable JavaScript to edit and type-check this example.

Check your diagnosis

TS2322 says a string cannot be assigned to a number. For a numeric literal, remove the quotation marks. For real text input, use Number(text) and validate its result; later lessons cover that boundary.

Your turn

Exercise

Hello, TypeScript: practice

A study plan has 4 sessions of 25 minutes plus one 15-minute review at the end. Declare a number for each of those three facts and print the total time, 115. Use explicit number annotations for this first exercise.

Your workspaceTypeScript
// Declare sessions, minutesPerSession and reviewMinutes, then print the total.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

The types describe the inputs. Multiplication is still ordinary JavaScript.

Hint 2: a first step

Start by declaring sessions as a number with value 4, then work out which operator joins each pair of values.

Show one solution
One possible solutionTypeScript
const sessions: number = 4;
const minutesPerSession: number = 25;
const reviewMinutes: number = 15;

console.log(sessions * minutesPerSession + reviewMinutes);

Enable JavaScript to edit and type-check this example.

Text
115

Multiplication runs before addition, so this is 100 + 15. The : number annotations let the checker reject const reviewMinutes: number = "15" before the program runs, but it would accept sessions + minutesPerSession + reviewMinutes (44): choosing the right operator is your job, and only the expected 115 catches that mistake.

Change it and explain it

Change sessions to zero, then to a negative number. Both pass type checking. Explain why a study planner might still reject the negative value. Spend the last two minutes writing the difference between a type annotation and Number(text).

Was this article helpful?

Your response is saved on this device.