ProgramGuru

Your First TypeScript Program

Learn Your First TypeScript Program

Your first program should do one observable thing: print a greeting without any input or external dependency.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind your first typescript program.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Save source code as hello.ts. Run a type check with npx tsc --strict --noEmit hello.ts. To create JavaScript, run npx tsc --target es2022 --module commonjs hello.ts; then run node hello.js. A browser-based TypeScript Playground is an alternative for quick learning.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

Your First TypeScript Program: illustrated flowconst greeting = ...; Read greeting value; console.log(value); Visible message1const greeting = ...2Read greeting value3console.log(value)4Visible messageFollow the change from source values to the final result.
Your First TypeScript Program: illustrated flow. const greeting = ...; Read greeting value; console.log(value); Visible message.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const greeting: string = "Hello, TypeScript!";
 console.log(greeting);

Verified output

Hello, TypeScript!

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

First the string is stored in greeting. Then console.log prints that value. The surrounding quotation marks define a string in code; the output contains the text, not the quote characters.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

TypeScript source normally needs compilation or a compatible runtime before execution. Printing a greeting does not prove type checking occurred.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Change the message to include your favorite activity. Predict the exact output and compare it to the console.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

What does console.log(greeting) print?
Choose one answer. You can try again.

Answer and explanation

The contents of the greeting string.

console.log receives the variable value and prints it.

Review the key idea

Your First TypeScript Program: Save source code as hello.

console.log receives the variable value and prints it.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Structure of a TypeScript Program →