Typescript Datatypes - tutorials with examples

typescript data types - Strings, Enums

Once you read Typescript Installation and Hello world program article, You will get a clear understanding of how to install typescript and write the hello world program.

In this article, I will walk through the basic data types of typescript language.

Typescript datatype

Typescript has all data types that support javascript.

Supported data types in typescript are void, Null, undefined, boolean, string, Number, array , tuple data types, Enum, Any and Never data types.


Variables can be declared using the below syntax.

[let/var/const] variablename: [datatype];  

variable names are valid names datatypes are either built-in types or custom data types as objects. let var, and const are keywords to declare variables based on the variable scope variable.

How to declare Boolean data type in Typescript?

Boolean is a basic data type that contains true or false values. The same is also available in javascript as well as other programming languages like java. Boolean types are used in if-else and while logical flows.

let isChecked: boolean;  // valid  
let isFlag:boolean=true; // valid  
let isValid1:boolean=213; // Not valid and gives compile time error  
let isValid2:boolean='test'; // Not valid and gives compile time error  

isValid1 and isValid2 are not valid values. and gives Type ‘213’ is not assignable to type ‘boolean’ compile-time error.

Declare Number

Typescript does not have an integer or long data type, but it does have a ’number’ data type that defaults to a floating number. We can represent integer and floating values in the number data type.

let n1: number = 123; // valid  
let n2: number = 0o987; // Octal  
let n3: number = 0xbac01; // Hexadecimal  
let n4: number = "string"; // Not Valid. i.e Error  
let n5: number = null; //  Valid  
let n6: number = undefined; // Valid  

String data type

The string is a common data type in any programming language. It represents a group of characters stored under a variable of String type. The string can be declared using double quotes or single quotes. Please see String Complete examples

let s1: string = undefined; // valid  
let s2: string = null; // valid  
let s3 : string = ""; // valid  
let s4: string = "building"; // valid  
let s5: string = 12; //  Not valid  

How to declare Arrays type Object in typescript

Arrays are important data types available in javascript and typescript.

Arrays group multiple values with a single name and the size of an array is not known.

Arrays are one of the data types in typescript collection. Arrays can be created in multiple ways like Generic Arrays or typed Arrays

let numerArrays: number[] = [11, 2, 10]; // Number Generic  Arrays  
let stringArrays: string[] = ["jan", "Feb", "Mar","Apr"]; // valid array  
let numberArrays: number[] = [1, "Jan", "Feb"]; // Not valid array  
let months: Array = ['January', 'February', 'March']; // Typed array declaration

Tuple Datatype

Tuple data type is used to create a fixed group of different data type values. Data types can be different.

let dataMix: [number, string];  
dataMix = [2000,'Jan salary'];  

Enum Datatype

Enum is to represent the collection values stored under one name called Enumeration of items. It is the Enumeration of items.

enum Diagram {Rectangle, Circle, Square};  
let myFirstDiagram: Diagram = Diagram.Rectangle;  

please see Detailed Enum tutorials with all examples

Void Datatype

void means nothing. The void is used in the return type of methods/functions. Void in the method means doesn’t return anything.

function testVoidMethod(): void {  
    console.log("this is to test Void data type")  

Undefined Datatype

In javascript, if any variable is not initialized with any value, by default, it initiates with undefined.

We can also assign an undefined value.

let numberUndefined: number = undefined; // valid  
let voidUndefined: void = undefined; // Valid  

Any and Never data type

any data type is used to hold the values of different data types.

let stringValue: any = "anyvalue";  
let booleanValue: any = true;  
let arrayValues: any[] = ["anyvalue",  true, null, 986,];  
let numberValue: any = 54;  

Never data type is used to specify the data/function flow will never run.

It uses function return types as well as variables.

function infiniteMethod(): never {  
    while (true) {  
    return 'this line never runes ';  

Please check other articles on typescript tutorial series

You can check Typescript Related Posts:

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.