AI Hints
Home›TypeScript Tutorial›First Steps
TypeScript · Lesson 4 of 120

Your First Program

Hello, TypeScript.

⏱ 2 min read▶ Video at 3:15🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Let's write a real TypeScript program. A few variables with types, and a message built from them. The syntax is a colon, then the type, right after the name.

Your First Program
From the video: Your First Program
  • A few variables
  • Colon, then the type
  • A message

Hello, TypeScript

Shop is a string, bikes is a number, and open is a boolean, meaning true or false. Then we print a message using all three. Run it. TypeScript checked every type, and the program ran.

app.tstypescript
const shop: string = "Northwind Bikes";
const bikes: number = 12;
const open: boolean = true;

console.log(`${shop} has ${bikes} bikes in stock.`);
console.log("Open today?", open);
Output
Northwind Bikes has 12 bikes in stock.
Open today? true

The pattern

Name, colon, type, equals, value. Const means the variable won't be reassigned. Let means it can change. Template strings, with backticks, put values inside text.

keyvalue
Patternname: type = value
constnever reassigned
letcan change
`${x}`values in text

Summary

Write a colon and a type after a variable's name. Next, reading type errors.

Key takeaways
  • name: type
  • const and let
  • Template strings
Next up: Reading Type Errors — what tsc tells you