Inference and type annotations
Variables with inferred types
let count = 10;
let message = "Hello World!";
let isActive = true;
let items = [1, 2, 3, 4, 5];
let user = { name: "Alice", age: 30 };
Variables with Type Annotations (explicit types)
let total: number = 100;
let greeting: string = "Welcome!";
let isLoggedIn: boolean = false;
let numbers: number[] = [10, 20, 30];
let fruits: Array<string> = ["apple", "banana", "cherry"];
let profile: { username: string; email: string } = { username: "bob", email: "bob@example.com" };
Functions with inferred return types
function greetUser(name: string) {
return `Hi, ${name}!`;
}
const multiply = (x: number, y: number) => x * y;
function add(a: number, b: number) {
return a + b;
}
Not all functions have inferred return types
declare function format(value: number): string;
function addNumbers(a, b) {
return a + b;
}
function addValues(a: number, b: string) {
return a + b;
}
function greetPerson(name) {
return `Hi, ${name}!`;
}
function multiplyValues(a: number, b: number) {
return format(a * b);
}