Intro to Typescript-2
Tanzim Ibthesam
Posted on March 9, 2021
This is a sequel to Intro to Typescript part-1.If you haven't looked up it please take a look. Here I will discuss about Functions,Type Aliases,Function Signatures
Function
Here we will learn how to define a function in typescript
const info=(name:string,age:number,phone?:number | string )=>{
console.log(name);
console.log(age);
console.log(phone);
}
info("Tanzim",20.34,"0145566987");
Here we see we can define type of the parameters in a function
In the info function in parameter phone we can select whether we can say that it will be either number or string through ? string|number
Suppose we want to assign any type of parameter to a function we can just write
number,phone:any
But any is not at all recommended.Its just written here to show.
If we define variable as a function
let sum=(num1:number,num2:number)=>{
console.log(num1+num2);
}
let totalsum=sum(20,20);
Here if we try to resign value of totalsum as string we get error
totalsum='20';
We will get this
totalsum is not assignable to type void
TypeAliases
type userinfo = { name: string, email: string }
const greet=(user:userinfo)=>{
console.log(`${user.name}and ${user.email}`);
}
Here you can see how to define type alias through which we can take all types in a function.
I tried using var,let or const by mistake but we need to use type if we need to use the same parameters over and over again. Type is equal to the parameters along with their types.
Function Signatures
We can define signature for a function what the type of parameters will be in a function and what will be its signatures
let newprofile:(name:string,age:number)=>void;
newprofile=(name:string,age:number)=>{
console.log(`His name is ${name} and age is ${age}`);
}
newprofile("Tanzim",25);
Here at first we defined the signature that is both parameters of function will be string and number. If we try to assign Boolean to any parameter it will give us an error.
let newprofile:(name:string,age:number)=>void;
newprofile=(name:string,age:boolean)=>{
console.log(`His name is ${name} and age is ${age}`);
}
newprofile("Tanzim",true);
The error it will give us is this
In second case we see
let totalnum:(numone:number,numtwo:number,action:string)=>number;
totalnum=((numone:number,numtwo:number,action:string)=>{
if(action=="added"){
return numone+numtwo;
}else{
return numone*numtwo;
}
});
here if we try to change a parameter to string or boolean which will not return number we will get error.
In third case
let userinfo:(obj:{name:string,age:number})=>void;
type usersinfotype={name:string,age:number};
userinfo=(usersinfo:usersinfotype)=>{
console.log(`His name is ${usersinfo.name} and age is ${usersinfo.age}`);
}
userinfo({name:'Tanzim',age:25})
He we returned parameter as an object. If we wrote type
type usersinfotype={name:string,age:boolean};
It would give us an error saying void is not assignable to type.
So here I discussed about alias,functions,function signatures in next part I will discuss about Interfaces and further advance things
Posted on March 9, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 26, 2024
November 26, 2024