Sunday, 7 May 2017

Working with Types in Typescript

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, i.e.




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 above example, we declared name variable in if condition, but we able to access the same variable out of the if condition because var is globally available in a function block.
But let is accessible in nearest enclosing block (or globally if declared outside of a function). If we try to access the variable outside of the block, we will get 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 below example, we got an error stating ‘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 above image, 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 have 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 super of all data types in typescript. It is the default data type for variables and function.
In the above example, 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. But In the case of any data type, we can store any type of value.
TypeScript also provides three types (void, undefined, null) that are not used as type annotation, but used in case of absence of values. Void Type is used only for functions which do not return a value.

Undefined type is the value of a variable that has not 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 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 requirement.
An example of Enum:
In the above example, we have created an enum. As enum is zero based, the value of the first enumerator is 0 and then it increases by 1. After that, 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 below example, we set 5 as starting value first enumerator.
We can even override the value of each enumerator of an enum.
Tuples: Tuple represents the heterogeneous collection of values. Tuple values are individually called as items. In below example, we have created a tuple which can hold values of type string and number.
If we try to store a value of another type which is not specified then we will get an error.
Hope this will help you. Thanks.

0 comments:

Post a Comment

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+