Understanding Basic types in Typescript Tutorials

Basic types include Boolean (bool), Number, String, Array, Enum etc. in TypeScript. We will be exploring them to know that TypeScript is ‘typed superset’ of JavaScript.

JavaScript – Optional types & its effect

  • JavaScript is dynamically typed; variables do not have an associated type, so no type restrictions can be applied to operations.
  • We can assign a value of one type to a variable and later assign a value of a completely different type to
    the same variable (Comment 1 in code block below).
  • We can perform an operation with two incompatible values and get unpredictable results (comment 2 in code block below).
  • If we call a function, there is nothing to enforce that you pass arguments of the correct type and you can even supply too many or too few arguments(comment 3, 4 in code below).

Let’s see working example of this in JavaScript, I hope you have Setup Visual Studio Code for TypeScript tutorials.

Create basictypes.js file and copy below code to explore ill effects of dynamically typed JavaScript

Now just copy the above JavaScript code into TypeScript file, create “basictypes.ts“.  You would see similar screen shot of it

basic types

Type Checking

With this small example of how types is checked in TypeScript, lets learn basic types in this article.

Basic Types in TypeScript

Boolean

This is most basic datatype which is has either TRUE or FALSE.

Number

As in JavaScript, all numbers in TypeScript are floating point values. These floating point numbers get the type number. In addition to hexadecimal and decimal literals, TypeScript also supports binary and octal literals introduced in ECMAScript 2015.

String

There is no application, technology, language which does not deal with textual data or strings. Keyword ‘string‘ is used to represent textual data in TypeScript also. They are represented in double quotes or single quotes.

The concept of template strings, is widely used and esp in Angular 2 we can HTML written as template string in Component also.

What is template strings? String which span multiple lines and have embedded expressions in them.

Template strings are surrounded by the backtick/backquote (`) character, and embedded expressions are of the form ${ expr }.

basic types

Template String code & output

Arrays

An array is simply marked with the [] notation, similar to JavaScript, and each array can be strongly typed to hold a specific type. Below code has different ways of using Arrays.

Enums

Enums are a special type that has been borrowed from other languages such as C#, and provide a solution to the problem of special numbers. An enum associates a human-readable name for a specific number.

By default, enums begin numbering their members starting at 0. You can change this by manually setting the value of one of its members or you can manually set all values in them.

In next article of TypeScript Tutorials series, we will explore in detail about “variable declarations, scoping using var & let keywords

You may also like...

1 Response

  1. May 25, 2016

    […] TypeScript Tutorials – Understanding Basic types in Typescript. Basic types include Boolean (bool), Number, String, Array, Enum etc. in TypeScript. We will be exploring them to know that TypeScript is ‘typed superset’ of JavaScript. JavaScript – Optional types & its effect JavaScript is dynamically typed; variables do not have an associated type, so no type restrictions can be applied to operations.We can assign a value of one type to a variable and later assign a value of a completely different type to the same variable (Comment 1 in code block below).We can perform an operation with two incompatible values and get unpredictable results (comment 2 in code block below).If we call a function, there is nothing to enforce that you pass arguments of the correct type and you can even supply too many or too few arguments(comment 3, 4 in code below). […]

Leave a Reply