Types in Typescript

Types in Typescript

Typescript is a strongly typed language, whereas javascript is not. A variable which has a value defined as a string can be changed to a number without any issues in Javascript. The same is not tolerated in typescript. In typescript, the type to a variable is defined at the start only and through the execution, it has to maintain the same type any changes to it will lead to a compile-time error during compilation to javascript.

Following are the types :

  • Number
  • String
  • Boolean
  • Any
  • Void

Number:

will take only integers, floats, fractions, etc.

Syntax:

let a :number = 10;
let marks :number = 150;
let price :number = 10.2;

Here are some important methods which can be used on Number types:

toFixed() – it will convert the number to a string and will keep decimal places given to the method.

toString() – this method will convert number to a string.

valueOf() – this method will give back the primitive value of the number.

toPrecision() – this method will format the number to a specified length.

Example : with all String methods

let _num :number = 10.345;
_num.toFixed(2); // "10.35"
_num.valueOf(); // 10.345
_num.toString(); // "10.345"
_num.toPrecision(2); //"10"

String

String: only string values

Syntax:

let str :string = "hello world";

Here are some important methods which can be used on String types:

  • split() – this method will split the string into an array.
  • charAt() – this method will give the first character for the index given.
  • indexOf() – this method will give the position of the first occurrence for the value given to it.
  • Replace () – this method takes 2 strings, first the value to search in the string and if present will replace it will the 2nd one and will give a new string back.
  • Trim () – this method will remove white spaces from both sides of the string.
  • substr() – this method will give a part of the string which will depend on the position given as start and end.
  • substring() – this method will give a part of the string which will depend on the position given as start and end. The character at the end position will be excluded.
  • toUpperCase() -will convert the string to uppercase
  • toLowerCase() – will convert the string to lowercase.

Example:

let _str:string = "Typescript";

_str.charAt(1); // y
_str.split(""); //["T", "y", "p", "e", "s", "c", "r", "i", "p", "t"]
_str.indexOf("s"); //4 , gives -1 is the value does not exist in the string.
_str.replace("Type", "Coffee"); //"Coffeescript"
_str.trim(); //"Typescript"
_str.substr(4, _str.length); //"script"
_str.substring(4, 10); //"script"
_str.toUpperCase();//"TYPESCRIPT"
_str.toLowerCase();//"typescript"

Boolean

Will accept logical values like true, false, 0, and 1.

Syntax:

let bflag :boolean = 1;
let status :boolean = true;

Any

Syntax:

let a :any = 123
a = "hello world"; // changing type will not give any error.

Variables declared using any type can take the variable as a string, number, array, boolean or void. Typescript will not throw any compile-time error; this is similar to the variables declared in JavaScript. Make use of any type variable only when you aren’t sure about the type of value which will be associated with that variable.

Void

Void type is mostly used as a return type on a function which does not have anything to return.

Syntax:

function testfunc():void{
 //code here
}

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email