Types in Typescript

Every programming language must have data types defined to efficiently store, access data objects and identify the code errors at the compile time. JavaScript and TypeScript is not an exception. In this, article we will dive into primitive data types and complex data types in TypeScript.

JavaScript is a dynamic programming language, which means, the data types are assigned dynamically to the variables at run time. However, Opposed to the javaScript, TypeScript adds static language paradigms and lean towards object-oriented programming style.

Like most of the programming languages, data types in TypeScript can be broadly categorized into,

Primitive Data Types

Primitive data types are basic language data types that comes with language. In TypeScript or JavaScript world, string primitive data type is much simplified than other programming languages like java. For example, java has char, String etc. But JavaScript and TypeScript has only one data type string. Let’s take a look at each primitive data types in TypeScript.

string

String is a primitive data type in TypeScript. If you are coming from other languages, there is no type called char. We can create a string and assign it to a variable as shown below.

Note: We can use " (double quotes) and ' (single quote) interchangeably in TypeScript.

Note: There is no char type in typescript.

Example

const name: string = 'sde.whiletrue.live';
const auther: string = "author";

number

Number is a simplified primitive data type in TypeScript. In TypeScript or JavaScript world, primitive data types are much simplified than other programming languages like java. For example, java has short, long, int, byte, etc. But JavaScript and TypeScript has only one data type number. See - it is simple 😉.

Note: number Datatype in TypeScript is simple. There is no float, double, int, long, short and byte data types in TypeScript.

Example

const age: number = 28;
const height: number = 172.5;

boolean

Boolean is a yes or no kind of data type. Boolean data type can have true and false as values.

Example

const alive: boolean = true;

void

void is used where there is no data and when a function does not return anything. It is used alongside functions.;

Note: There is no meaning to assign void to a variable. Only null or undefined is assignable to void.

Example:

function doSomething(): void { 
    console.log('didSomething!')
} 

let speech: void = doSomething(); 
console.log(speech); //Output: undefined

It does not make sense to do as shown below

// void cant be used this way
let doNothing: void = undefined; // Error
let age: void = null;  // Error

undefined

undefined is a property of the global object. It is a variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned. It can be better explained with an example

Example:

let item;
if (item === undefined) {
  // these statements execute
} else {
  // these statements do not execute
}

Note: The strict equality operator (as opposed to the standard equality operator) must be used here. It is Because x == undefined also checks whether x is null, while strict equality doesn’t.

This is because null is not equivalent to undefined.

Undefined type check can be done using void too.

Example:

let item;
if (item === void 0) {
  //  these statements execute
}

//  item has not been declared before
if (item === void 0) {
  //  throws Uncaught ReferenceError: y is not defined
}

null

The null type is the second primitive data type that also has only one value null.

Example:

let item = null;
console.log(typeof item); // object

Note: The typeof null returns object is a known bug in JavaScript. A proposal to fix this was proposed but rejected. The reason was the that fix would break a lot of existing sites.

JavaScript defines null as is equal to undefined.

Example:

console.log(null == undefined); // true

never

TypeScript introduced a new type called never. It indicates the values that will never occur.

The never type is used when you are sure that something is never going to occur. For example, you write a function which will always throws an exception.

Example: The throwError() function throws an error and doSomethingForever() function is always executes forever and never reaches an end point because the while loop never ends.

Never type is used to indicate the value that will never occur or return from a function.

function throwError(errorMsg: string): never { 
            throw new Error(errorMsg); 
} 

function doSomethingForever(): never { 
            while (true) { 
         console.log('I always does something and never ends.');
     }
}

Complex Data Types

Complex data types are built upon the primitive data types. Any instance of a user defined class can be called a complex data type.

function

In TypeScript or JavaScript, functions are first class objects. That means, a function are treated as object and pass to a variable and as function parameter.

And so, functions can be treated as a data type in typescript

Example:

const greet = (name: string) => {
  return 'Hello ' + name;
}

Any

TypeScript has type-checking and compile-time checks. However, we do not always have prior knowledge about the type of some variables, especially when there are user-entered values from third party libraries. In such cases, we need to deal with dynamic content. The Any type comes in handy here.

Any is like super type to all the data types. (It is like Object data type in java)

Example:

let greet: any = "Hey Dude!"; 
greet = 19;
greet = true;

The above TypeScript code will compile into the JavaScript as shown below

var something = "Hello World!";
something = 23;
something = true;

classes

Classes when they are instantiated, object become the type of the class from which they are created.

Example:

class Country {

}

const country: Country = Country();

Here, country is of type Country.

User Defined Datatypes

Any class that we create are derived from the primitive types and form a user defined type.

let date: Date = new Date();

Date is user defined and a complex data type. It has attributes (variables) and methods (functions).

typescript programming types primitives

Subscribe For More Content