← Back to TypeScript Course | Chapter 2: Basic Types | Lesson 1 of 11

Number, String, Boolean

TypeScript provides number, string, and boolean types for common values. These basic types help TypeScript check that variables contain the kind of data you expect.

Number Type

The number type is used for both integer and decimal values, since TypeScript, following JavaScript, has just one numeric type instead of separate int and float types. This means 42 and 3.14 are both perfectly valid number values with no special casting needed.

Example: Number Type

typescript
let age: number = 42;
let price: number = 3.14;
console.log(age, price);

String Type

The string type is used for text values, and TypeScript accepts strings written with single quotes, double quotes, or backtick template literals interchangeably. The type itself doesn't care which quoting style you use, only that the value is text.

Example: String Type

typescript
let a: string = 'single';
let b: string = "double";
let c: string = `backtick`;
console.log(a, b, c);

Boolean Type

The boolean type represents exactly one of two values, true or false, and is most commonly used for conditions, flags, and yes-or-no states. TypeScript will reject anything else, like the string "true", from being assigned to a boolean-typed variable.

Example: Boolean Type

typescript
let isActive: boolean = true;
// isActive = "true"; // rejected: string is not boolean
console.log(isActive);

Type Checking

TypeScript checks every assigned value against its declared type at compile time, so assigning an incompatible value, like a string to a number-typed variable, produces an error before the code runs. This catches an entire category of bugs that plain JavaScript would only surface at runtime, if at all.

Example: Type Checking

typescript
let count: number = 5;
// count = "5"; // compile-time error: string assigned to number
console.log(count);

Using Basic Types Together

Number, string, and boolean types are frequently combined to describe simple program data, such as a user record with an id (number), a name (string), and an isActive flag (boolean). Explicit annotations on each field make the intended shape of that data unmistakable to anyone reading the code.

Example: Using Basic Types Together

typescript
interface User {
  id: number;
  name: string;
  isActive: boolean;
}
const user: User = { id: 1, name: "Zoya", isActive: true };
console.log(user);
🔒

Chapter Quiz — Complete all 11 topics to unlock

0/11 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.