AI Hints
Home›TypeScript Tutorial›Basic Types
TypeScript · Lesson 7 of 120

Basic Types

String, number, boolean.

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

Most values are one of three basic types. Strings hold text. Numbers hold any number, whole or decimal. And booleans hold true or false.

Basic Types
From the video: Basic Types
  • string: text
  • number: any number
  • boolean: true or false

The three basics

Model is a string, price is a number with decimals, and in stock is a boolean. Methods follow the type. Strings can be upper cased, numbers can be rounded to two decimals. Run it.

app.tstypescript
const model: string = "Trail 500";
const price: number = 849.5;
const inStock: boolean = true;

console.log(model.toUpperCase());
console.log(price.toFixed(2));
console.log(inStock ? "Available" : "Sold out");
Output
TRAIL 500
849.50
Available

Methods follow the type

Because price is a number, TypeScript knows it has no upper case method. Run it. An error, before the program could crash.

app.tstypescript
const price: number = 849.5;
console.log(price.toUpperCase());
Output
app.ts line 2: Property 'toUpperCase' does not exist on type 'number'.

Summary

Strings, numbers, and booleans are the basic types, and each has its own methods. Next, arrays.

Key takeaways
  • string
  • number
  • boolean
Next up: Arrays — lists of one type