Basic Types: String, Number, Boolean
TypeScript has various basic (primitive) types. Common ones include string, number, and boolean. These are the fundamentals of TypeScript's type system.
The string type
A string represents text data, it can be a single character, a word, sentance, or paragraph. In TypeScript, you can use single quotes, double quotes, or template literals (backticks):
Loading code...
let firstName: string = "Homer";
let lastName: string = 'Simpson';
let message: string = `Hello, ${firstName}!`;
console.log(message);
String methods
All regular JavaScript string methods are available in TypeScript, and TypeScript knows the return types:
Loading code...
let text: string = "Hello, TypeScript!";
text.toUpperCase(); // Returns: string
text.length; // Returns: number
console.log(typeof(text.toUpperCase()));
console.log(typeof(text.length));
TypeScript will warn you if you try to use a method that doesn't exist:
Loading code...
let text: string = "Hello, TypeScript!";
text.toUppercase(); // typo- should be toUpperCase()
The number type
TypeScript has only one number type (unlike some languages that have int, float, double, etc). Used for both integers and floating-point numbers:
let age: number = 40;
let count: number = 7;
let temperature: number = -5.5;
let cost: number = 29.99;
Number operations
TypeScript understands number operations and also their return types:
Loading code...
let a: number = 10;
let b: number = 5;
let add: number = a + b; // 15
let subtract: number = a - b; // 5
let multiply: number = a * b; // 50
let divide: number = a / b; // 2
let remainder: number = a % b; // 0
console.log(add)
console.log(typeof(add))
Number methods
Numbers have methods available through the Number object:
Loading code...
let num: number = 3.14159;
console.log(typeof(Number.parseInt("42"))); // number
console.log(typeof(Number.parseFloat("3.14"))); // number
console.log(typeof(num.toFixed(2))); // string
toFixed()formats a number to a specific number of decimal places. Resulting in a string value (e.g. "3.14" instead of 3.14)
The boolean type
A boolean type represents a true or false value:
let isLoggedIn: boolean = true;
let admin: boolean = false;
let hasPermission: boolean = true;
Boolean operations
You can use logical operators with booleans:
Loading code...
let isLoggedIn: boolean = true;
let isAdmin: boolean = false;
let canEdit: boolean = isLoggedIn && isAdmin; // false
let canView: boolean = isLoggedIn || isAdmin; // true
console.log(canEdit);
console.log(canView);
Type safety examples
TypeScript prevents common mistakes related to types:
Loading code...
// Strings
let name: string = "Marge";
name = 42; // Error: Type 'number' is not assignable to type 'string'
// Numbers
let age: number = 30;
age = "thirty"; // Error: Type 'string' is not assignable to type 'number'
// Booleans
let isActive: boolean = true;
isActive = "false"; // Error: Type 'string' is not assignable to type 'boolean'
Combining types in functions
You can use these types in function parameters and return types:
function welcome(name: string): string {
return `Hello, ${name}!`;
}
function canOrder(isAvailable: boolean, hasIngredients: boolean): boolean {
return isAvailable && hasIngredients;
}
function calculateTotal(price: number, quantity: number): number {
return price * quantity;
}
(name: string)this specifies the name passed to the function must be a string: stringspecifies a string as the function return type
These three types (string, number, boolean) are the most fundamental types in TypeScript. In the next lesson, we will explore the types of null, undefined, void, and never.