DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. 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.

Anoop Kumar Sharma user avatar by
Anoop Kumar Sharma
CORE ·
May. 09, 17 · Tutorial
Like (6)
Save
Tweet
Share
8.10K Views

Join the DZone community and get the full member experience.

Join For Free

Declare Variables in TypeScript:

Before using the Type Annotation, let’s take a look at declaring a variable using the var, let , and const keywords.

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.

But 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 let and const.

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:

TypeScript adds static typing, using type annotation which enables type checking at the compile-check and also adds a 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 (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.

TypeScript Data Types

Published at DZone with permission of Anoop Kumar Sharma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Future of Cloud Engineering Evolves
  • Secrets Management
  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • DevOps Roadmap for 2022

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: