Learn TypeScript programming with simple, clear examples
CONTENTS
Learn TypeScript through examples. Each section shows a concept with runnable code.
Click the “Run” button to execute examples and see output.
TypeScript adds types to JavaScript. Specify the type after a colon.
let name: string = "Alice";
let age: number = 25;
Numbers are numbers. Add. Subtract. Multiply. Divide.
let x: number = 10;
let y: number = 3;
let sum: number = x + y;
Calculate 15 * 4 and store it in a variable called result
with type number
.
Strings are text. Words. Letters. Characters.
let greeting: string = "Hello";
let name: string = "World";
let message: string = greeting + " " + name;
Create a string called sentence
that says “TypeScript is great”.
Arrays hold multiple items. Ordered. Indexed. Typed.
let fruits: string[] = ["apple", "banana", "orange"];
let firstFruit: string = fruits[0];
Create an array called colors
with type string[]
containing “red”, “green”, and “blue”.
Functions do things. Take input. Return output. Typed.
function greet(name: string): string {
return "Hello, " + name;
}
let message: string = greet("Alice");
Create a function called add
that takes two numbers and returns their sum.
Loops repeat. For each item. Do something.
let numbers: number[] = [1, 2, 3, 4, 5];
for (let num of numbers) {
console.log(num * 2);
}
Print each number from 1 to 5.
Conditions check. If true, do this. If false, do that.
let age: number = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
Check if a number is positive. Print “Positive” or “Negative”.
Objects map keys to values. Look up. Find. Typed.
interface Person {
name: string;
age: number;
city: string;
}
let person: Person = {
name: "Alice",
age: 25,
city: "New York"
};
console.log(person.name);
Create an interface called Book
with title
and author
properties.
Classes create objects. Blueprint. Template. Typed.
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet(): string {
return "Hello, I'm " + this.name;
}
}
let alice: Person = new Person("Alice");
console.log(alice.greet());
Create a class called Car
with a brand
property and a start
method.
Errors happen. Catch them. Handle them.
try {
let result: number = 10 / 0;
} catch (error) {
console.log("Cannot divide by zero");
}
Try to access an index that doesn’t exist in an array. Catch the error.
Generics make code reusable. Type-safe. Flexible.
function getFirst<T>(items: T[]): T {
return items[0];
}
let firstNumber: number = getFirst([1, 2, 3]);
let firstString: string = getFirst(["a", "b", "c"]);
Create a generic function called getLast
that returns the last item in an array.
Create a simple program. Use everything you learned.
interface CalculatorHistory {
operation: string;
result: number;
}
class Calculator {
private history: CalculatorHistory[] = [];
add(a: number, b: number): number {
let result: number = a + b;
this.history.push({ operation: `${a} + ${b}`, result });
return result;
}
showHistory(): void {
for (let entry of this.history) {
console.log(`${entry.operation} = ${entry.result}`);
}
}
}
let calc: Calculator = new Calculator();
calc.add(5, 3);
calc.add(10, 20);
calc.showHistory();
Create your own calculator with multiply and divide methods.
Conclusion
You learned TypeScript. Variables. Functions. Classes. Loops. Conditions.
Simple. Clear. Direct.
TypeScript is powerful. You are ready.