📘
Basic Types and Type Annotations
Beginner
80 XP
30 min
Lesson Content
TypeScript Basic Types
TypeScript provides several basic types that you can use to annotate your variables, function parameters, and return values.
Primitive Types
// String
let name: string = "John";
// Number
let age: number = 30;
// Boolean
let isActive: boolean = true;
// Null and Undefined
let value: null = null;
let data: undefined = undefined;Arrays
// Array of numbers
let numbers: number[] = [1, 2, 3];
// Alternative syntax
let names: Array = ["Alice", "Bob"]; Any Type
// Use sparingly - disables type checking
let anything: any = "can be anything";
anything = 42;
anything = true;Void Type
// For functions that don't return anything
function logMessage(message: string): void {
console.log(message);
}Type Inference
// TypeScript can infer types
let count = 10; // inferred as number
let greeting = "Hello"; // inferred as stringExample Code
Practice using different TypeScript types
// Declare variables with proper types
let userName: string = "TypeScript";
let userAge: number = 5;
let isLearning: boolean = true;
let skills: string[] = ["JavaScript", "TypeScript"];
console.log(`Name: ${userName}, Age: ${userAge}`);
console.log(`Learning: ${isLearning}`);
console.log(`Skills: ${skills.join(", ")}`);Expected Output:
Name: TypeScript, Age: 5 Learning: true Skills: JavaScript, TypeScript
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code