In this article, we will learn how to declare variables and use type annotations in Typescript. If you are new to Typescript, I will recommend you to check the previous article of this series:
Declare variables in Typescript
Before using the type Annotation, let’s take a look at declaring a variable using var, let ,and const keywords.
var, let and const
var is globally available in function block and is hoisted to the top of the function.
In the example mentioned above, we declared name variable in if condition, but we are able to access the same variable out of the if condition because var is globally available in a function block.
It is let, which is accessible in the nearest enclosing block (or globally, if declared outside of a function). If we try to access the variable outside the block, we will get the reference error.
Let’s move the console.log (name) statement inside if condition and run the Application. It will print the name on the console.
We can redeclare var in the same function scope, but cannot declare let and const.
In the example given below, we got an error, which states we cannot redeclare block-scoped variable.
In the case of const, we even cannot assign again, as it is constant or read-only property.
Type annotation in Typescript
Typescript adds static typing, using type annotation, which enables type checking at the compile-check and also adds class-based OOPS concept to JavaScript. It is optional to define the variable data type.
In the image given above, the first syntax is with type-annotation as well as value. The second syntax is with type-annotation, but no value (have undefined value) and third syntax is without the type-annotation but has the value.
Typescript allows us to declare variables with the specific data type and can be classified into Built-In types (ex. String, number, Boolean, etc.) and user-defined types (ex. Array, Class, interface, enum etc.). Any data type is the super of all the data types in Typescript. It is the default data type for the variables and the function.
In the example given above, we declared 3 datatypes (i.e. string, number, boolean) and printed their value on the console. If I want to store a text/string in age field, then I can’t do so because the data type of age variable is a number. In the case of any data type, we can store any type of value.
Typescript also provides three types (void, undefined, null), which are not used as type annotation, but used in case of absence of values. Void type is used only for the functions, which do not return a value.
Undefined type is the value of a variable, which has not been assigned any value, whereas null type represents the intentional absence of an object value.
Array
Array type is defined by the name of the type followed by a pair of square brackets (i.e. shorthand method) or by using generic type (specify the type in angular brackets of an array i.e. longhand method. Ex Array<String>). We can get the items of an array by iterating a loop on its length.
Enum
Enum allows us to define a set of named numeric constant. By default, enum is zero-based but we can change its value according to the requirement.
An example of Enum is given below.
In the example given above, we have created an enum. As enum is zero based. The value of the first enumerator is 0 and then it increases by 1. Afterwards, I created a variable of an enum type (shirtSize) and printed its value on the console.
Enums are zero-based but we can start it from a specific value. In the example given below, we set 5 as starting value of the first enumerator.
We can even override the value of each enumerator of an enum.
Tuples
Tuple represents the heterogeneous collection of the values. Tuple values are individually called as the items. In the example given below, we have created a tuple, which can hold the values of the type string and the number.
If we try to store a value of another type, which is not specified, then we will get an error.
I hope this will help you. Thanks.