What is Type Annotations in TypeScript ?
Last Updated :
12 Feb, 2024
TypeScript Type Annotations are a way of explicitly typing the variables, functions, return values, parameters, or any other thing used in the TypeScript program. It provides the functionality to statically type the variables which makes it easier for developers to catch the type-related errors during the development instead of catching them at runtime. This mechanism of typing variables makes TypeScript a statically typed language. Type Annotations make it easier to debug applications and write a cleaner error-free code.
Example: The below code explains how you can explicitly type different parts of the TypeScript 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]
Share your thoughts in the comments
Please Login to comment...