Your first program
Hello main.ts
A TypeScript program is a plain text file with the ts extension. Open any text editor, type the two lines below, and save them as main.ts in an empty folder.
const message: string = "Hello, TypeScript"; console.log(message);
The first line stores text in a constant typed as string. The second line prints that constant to the terminal.
tsc main.ts
Compile the file by passing its name to tsc. A silent terminal means success: the compiler creates main.js next to main.ts and prints nothing at all.
tsc main.ts
Open main.js to see the result. The output is plain JavaScript with the string annotation erased, ready for any JavaScript runtime.
node main.js
Run the compiled file with node. TypeScript work ends at compile time, so from here on plain node executes the emitted JavaScript:
node main.js
Hello, TypeScript
The terminal prints Hello, TypeScript and nothing else. That short loop of edit, compile, and run drives every lesson that follows.
Read the first error
Break the program on purpose to meet the compiler. Replace the text with a number and compile again:
const message: string = 42;
main.ts(1,7): error TS2322: Type 'number' is not assignable to type 'string'.
Read the message from left to right. It names the file, the line and column in brackets, and the reason: a number was stored where a string was promised.
Fix the line, run tsc main.ts again, and confirm the terminal goes silent. That rhythm of reading the first error and fixing it is the core TypeScript skill.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
tsc main.tsleave in the folder when the program is correct?Answer
main.js, the same program as JavaScript without the annotationsThe compiler erases the type notes and writes the JavaScript file next to the source, with no declarations by default.
-
You run
tsc main.tsand the terminal prints nothing at all. What does that mean?Answer
The compile succeeded
tscreports errors and nothing else, so a silent terminal means a clean build. -
What does this program print?
const message: string = "Hello, TypeScript"; console.log(message);Answer
Hello, TypeScript
console.logprints the value of the string itself, without quotes. -
What does this program print?
const message: string = "Hello, TypeScript"; console.log(message.length);Answer
17
The string has 17 characters: five in Hello, the comma, the space and the ten of TypeScript.
-
What does the compiler print for this program?
const message: string = 42; console.log(message);Answer
error TS2322: Type 'number' is not assignable to type 'string'.
A number cannot be stored in a variable typed as string, and the compiler names both types.
-
The compiler prints
main.ts(1,7): error TS2322: .... What does the part in brackets name?Answer
The line and the column of the mistake in
main.tsThe position is written as line, column, so the mistake is on line 1 at column 7.
-
What does this program print?
const message = "Hello, TypeScript"; console.log(message.toUpperCase());Answer
HELLO, TYPESCRIPT
toUpperCasereturns a new upper case string, and the constant itself is not changed. -
You fix an error in
main.tsand runnode main.jswithout compiling again. What happens?Answer
The old JavaScript from the last compile runs
nodeonly seesmain.js, so the fix reaches the program after the nexttscrun. -
What does this program print?
const a = 2; const b = 3; console.log(a * b);Answer
6
Both constants are inferred as numbers from their values, and
*multiplies them. -
Where do you fix the mistake the compiler reports for
main.ts?Answer
In
main.ts, then compile it againThe source is the truth; the compiler rewrites
main.jsfrom it on the next run.
Article author: Arthur Isaev