Last Updated : 21 Jan, 2025
TypeScript Type Annotations allow developers to explicitly define the types of variables, functions, and other elements in the program.
const str: string = "GeeksforGeeks";
const num: number = 6;
const arr: (number | string)[] =
["GFG", "TypeScript", 500, 20];
console.log(typeof str);
console.log(typeof num);
console.log(arr);
Output:
string number ["GFG", "TypeScript", 500, 20]
More Examples of Type Annotations
Function with Type Annotations JavaScript
function greet(name: string): string {
return `Hello, ${name}!`;
}
const person: { name: string; age: number } = {
name: "Alice",
age: 30
};
const numbers: number[] = [1, 2, 3, 4, 5];
class Rectangle {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
area(): number {
return this.width * this.height;
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4