A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/typescript/what-is-type-annotations-in-typescript/ below:

What is Type Annotations in TypeScript ?

What is Type Annotations in TypeScript ?

Last Updated : 21 Jan, 2025

TypeScript Type Annotations allow developers to explicitly define the types of variables, functions, and other elements in the program.

JavaScript
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}!`;
}
Object with Type Annotations JavaScript
const person: { name: string; age: number } = {
    name: "Alice",
    age: 30
};
Array with Type Annotations JavaScript
const numbers: number[] = [1, 2, 3, 4, 5];
Class with Type Annotations JavaScript
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