Working With Types in TypeScript
Working With Types in TypeScript
If you're looking at getting started with TypeScript, read on to learn about it's declarative variables and type annotation.
Join the DZone community and get the full member experience.Join For Free
Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.
Declare Variables in TypeScript:
Before using the Type Annotation, let’s take a look at declaring a variable using the
let , and
var, let, and const:
varis globally available in a function block and is hoisted to the top of the function.
In the above example, we declared the variable name in the if condition, but we will be able to access the same variable outside of the if condition because
var is globally available in a function block.
let is accessible in the 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 a reference error.
Let’s move the
console.log(name) statement inside of the 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
In the below example, we got an error stating ‘cannot redeclare block-scoped variable.’
In the case of
const, we cannot even assign it again, as it is a constant or read-only property.
Type Annotation in TypeScript:
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 (has an undefined value) and the third syntax is without the type-annotation but has a 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 the age field, then I can’t do so because the data type of the 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 annotations but can be used in case of the 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 constants. By default, enum is zero-based, but we can change its value according to our requirements.
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 the below example, we set 5 as a starting value for our first enumerator.
We can even override the value of each enumerator of an enum.
Tuples: Tuples represent the heterogeneous collection of values. Tuple values are individually called as items. In the below example, we have created a tuple which can hold values of both type strings and numbers.
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.
Published at DZone with permission of Anoop Kumar Sharma , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.