DataTypes in TypeScript

  • Boolean: true/false value
  • Number: Use to define variable of type number
  • String: textual data
  • Array: an array is a collection of values of the same datatype.
  • Tuple: Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same.
  • Enum:  an enum is a way of giving more friendly names to sets of numeric values
  • Any: It is used to describe the type of variables that we do not know
  • Void: Used to represent the absence of having any type 
  • Null & Undefined: By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number
  • Never: never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns
  • Object: object is a type that represents the non-primitive type, i.e. anything that is not number, string, boolean, symbol, null, or undefined.
let isCompleted: boolean = false;
console.log(isCompleted);

let decimal: number = 26;
console.log(decimal);

let hex: number = 0xf00d;
console.log(hex);

let binary: number = 0b1010;
console.log(binary);

let octal: number = 0o744;
console.log(octal);

let firstName: string = "Girish";
console.log(firstName);

let message: string = `Hello, my name is ${ firstName }`;
console.log(message);

let num: number[] = [2, 4, 6];
console.log(num);

let num1: Array<number> = [8, 10, 12];
console.log(num1);

// Declare a tuple type
let myTuple: [string, number];

// Initialize it
myTuple = ["Girish", 30]; // OK
console.log(myTuple);

// Initialize it incorrectly
//myTuple = [30, "Girish"]; // Error

console.log(myTuple[0].substring(1)); // OK
//console.log(myTuple[1].substring(1)); // Error, 'number' does not have 'substring'

//myTuple[3] = "Test"; // Error, Property '3' does not exist on type '[string, number]'


enum WeekDays {Sun,Mon,Tue,Wed,Thu,Fri,Sat}
let w: WeekDays = WeekDays.Sun;
console.log(w);

enum WeekDays1 {Sun=1,Mon,Tue,Wed,Thu,Fri,Sat}
let w1: WeekDays1 = WeekDays1.Mon;
console.log(w1);

enum WeekDays2 {Sun=1,Mon=2,Tue=5,Wed=6,Thu=7,Fri=8,Sat=9}
let w2: String = WeekDays2[5];
console.log(w2);



let notSure: any = 28;
console.log(notSure);

notSure = "maybe a string instead";
console.log(notSure);

notSure = false; // okay, definitely a boolean
console.log(notSure);


let myList: any[] = [1, true, "Girish"];
console.log(myList);

myList[1] = 25;
console.log(myList);


function display(): void {
    console.log("This function returns void type");
}

let unusable: void = undefined;
unusable = null; // OK if `--strictNullChecks` is not given
console.log(unusable);

// Not much else we can assign to these variables!
let u: undefined = undefined;
console.log(u);

let n: null = null;
console.log(n);


// Function returning never must have unreachable end point
function error(message: string): never {
    throw new Error(message);
}

//error("Throw New Error");

// Inferred return type is never
function failed() {
    return error("Oops Something failed");
}
//failed()

// Function returning never must have unreachable end point
function infiniteWhileLoop(): never {
    while (true) {
    }
}
//infiniteWhileLoop()

References

  • https://www.typescriptlang.org/docs/handbook/basic-types.html

Learn more about TypeScript features in our upcoming blog articles

Happy Learning!