TypeScript Type Basics
TypeScript is nothing but JavaScript. TypeScript needs to be compiled but JavaScript does not. Also we can store any type of data in a variable in JavaScript but not in TypeScript.
JavaScript is cool but typescript is cooler.
A cooler feature of typescript is strictly typing. Now let’s see this cooler feature of typescript.
Strictly Typed
let a= “radioactive”
Here, ‘a’ is a variable that can hold a string. Because we set a string as its value.
If we want to modify it we can set another value that is a string.
let a= “radioactive”
a=”Raphael”
But if we modify it with a number type then,
let a= “radioactive”
a=20
Then your editor will show this error.
Type ‘number’ is not assignable to type ‘string’.
In TypeScript, when a value of the variable is set, its type will be the type of the value. You cannot set another type of value here.
Because TypeScript is strictly typed.
We can also set the type of variable like this,
let a:string= “radioactive”
Types
JavaScript types are used in TypeScript also,
Primitive Types
· String
· Number
· Undefined
· Null
· Symbol
· Boolean
And the others are objects,
· Functions
· Arrays
· Prototypes
There are some types those are exclusive to only TypeScript,
· Tuple — Tuple looks like an array, but for each index the type is fixed.
let tup: [number, string, string]= [24, “Radioactive”, “Raphael”]
If types are not matched, typescript will throw an error.
· Void (Functions with types) — JavaScript functions always return something. If nothing is returned then undefined is returned.
But in typescript, we can decide whether we will return something from the function or not. To stop returning (returning undefined), we can set void.
const returnName=(name: string): void =>{
console.log(name)
}
returnName(“Raphael”)
When we run this code this method will print the value of the parameter “name” and return undefined, as the return type is set as void. Here, as void is set as return type it will return undefined.
To return a string from the function. We can just set the return type of string.
const returnName=(name: string): string =>{
return name
}
console.log(returnName(“Raphael”))
· Any — We can make a variable that can hold any type of data using the any type. This type will make a variable loosely typed like normal JavaScript.
let a:any= “radioactive”
a=20
console.log(a) //20
As, we make the variable ‘a’ of any types, we first stored a string then modify this with a number.
Arrays
For arrays, an array signature with a type needs to be passed. For example,
let arr: number[]=[1,2,4]
console.log(arr)
Except for any,
let arr: any=[1,2,4]
console.log(arr)
Enums
Enum is a group of values that are associated with a numeric value.
enum info{
Radioactive,
Raphael
}
console.log(info.Radioactive) //0
console.log(info[0]) //Radioactive
console.log(typeof info[0]) //string
Like array, enums have indexing and it starts with 0. To get the any value of enum we just write the name of enum followed by the index value inside the square braces, just like we retrieve from the arrays.
Here are some basics about types. Types can be used in defining data types. It works for variables, arrays, functions. We can also set the return type of data of a function.