Coding for All
Lesson 1: Your First Type
Your First Type Add a type annotation and see what TypeScript is for.
1let score: number = 0;
2score = score + 10;
3console.log(score);
Terminal
$ npx tsc main.ts
$ node main.js
10
Every line, explained
let score: number = 0;
This is JavaScript with ONE new thing: the type annotation : number between the name and the =. It tells TypeScript that score may only ever hold numbers. If a later line tried score = "ten", TypeScript would refuse to build the program. The mistake would be caught while compiling, BEFORE the program ever runs. That is TypeScript's whole superpower.
score = score + 10;
Reassigning works exactly like in JavaScript: 0 + 10 = 10. The result is still a number, so TypeScript is happy.
console.log(score);
console.log(...) works exactly as it does in JavaScript; TypeScript IS JavaScript underneath, so everything from Intro to JavaScript still applies. Notice the terminal: TypeScript is first compiled to plain JavaScript (tsc), and THAT is what runs (node).