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.

  1. What does tsc main.ts leave in the folder when the program is correct?

    Answer

    main.js, the same program as JavaScript without the annotations

    The compiler erases the type notes and writes the JavaScript file next to the source, with no declarations by default.

  2. You run tsc main.ts and the terminal prints nothing at all. What does that mean?

    Answer

    The compile succeeded

    tsc reports errors and nothing else, so a silent terminal means a clean build.

  3. What does this program print? const message: string = "Hello, TypeScript"; console.log(message);

    Answer

    Hello, TypeScript

    console.log prints the value of the string itself, without quotes.

  4. 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.

  5. 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.

  6. 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.ts

    The position is written as line, column, so the mistake is on line 1 at column 7.

  7. What does this program print? const message = "Hello, TypeScript"; console.log(message.toUpperCase());

    Answer

    HELLO, TYPESCRIPT

    toUpperCase returns a new upper case string, and the constant itself is not changed.

  8. You fix an error in main.ts and run node main.js without compiling again. What happens?

    Answer

    The old JavaScript from the last compile runs

    node only sees main.js, so the fix reaches the program after the next tsc run.

  9. 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.

  10. Where do you fix the mistake the compiler reports for main.ts?

    Answer

    In main.ts, then compile it again

    The source is the truth; the compiler rewrites main.js from it on the next run.

Article author: Arthur Isaev